Skip to content

Commit e84348c

Browse files
committed
test(e2e): ensure valid config output format and commitlint success
1 parent 5f5cf52 commit e84348c

2 files changed

Lines changed: 200 additions & 0 deletions

File tree

tests/integration.test.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,43 @@ import { afterEach, beforeEach, describe, expect, test } from "vitest";
33
import { getCurrentPackageManager } from "./utils/package-manager";
44
import { TestEnvironment } from "./utils/test-environment";
55

6+
const expectGeneratedConfigToBeFormatted = async ({
7+
env,
8+
appPath,
9+
expectedGeneratedFiles,
10+
}: {
11+
env: TestEnvironment;
12+
appPath: string;
13+
expectedGeneratedFiles: string[];
14+
}) => {
15+
const generatedFilesThatExist = expectedGeneratedFiles.filter((filePath) =>
16+
env.fileExists(appPath, filePath),
17+
);
18+
const prettierCheckResult = await env.runPrettierCheckIfInstalled(
19+
appPath,
20+
generatedFilesThatExist,
21+
);
22+
expect(prettierCheckResult.success, prettierCheckResult.output).toBe(true);
23+
};
24+
25+
const expectCommitlintHookToAllowMessage = async ({
26+
env,
27+
appPath,
28+
message,
29+
}: {
30+
env: TestEnvironment;
31+
appPath: string;
32+
message: string;
33+
}) => {
34+
env.writeFile(
35+
appPath,
36+
"commitlint-hook-test.txt",
37+
`commitlint hook test ${Date.now()}\n`,
38+
);
39+
const commitResult = await env.commitWithMessage(appPath, message);
40+
expect(commitResult.success, commitResult.output).toBe(true);
41+
};
42+
643
describe("Next.js Integration Tests", () => {
744
let testEnv: TestEnvironment;
845
const packageManager = getCurrentPackageManager();
@@ -27,6 +64,18 @@ describe("Next.js Integration Tests", () => {
2764
const output = await env.runSolvroConfig(appPath, ["--all", "--force"]);
2865
expect(output).toContain("Konfiguracja zakończona pomyślnie");
2966

67+
await expectGeneratedConfigToBeFormatted({
68+
env,
69+
appPath,
70+
expectedGeneratedFiles: [
71+
"eslint.config.js",
72+
"package.json",
73+
".github/workflows/ci.yml",
74+
".github/dependabot.yml",
75+
".commitlintrc.js",
76+
],
77+
});
78+
3079
const prettierResult = await env.runPrettier(appPath, true);
3180
expect(prettierResult.success).toBe(true);
3281

@@ -77,6 +126,18 @@ describe("NestJS Integration Tests", () => {
77126
const output = await env.runSolvroConfig(appPath, ["--all", "--force"]);
78127
expect(output).toContain("Konfiguracja zakończona pomyślnie");
79128

129+
await expectGeneratedConfigToBeFormatted({
130+
env,
131+
appPath,
132+
expectedGeneratedFiles: [
133+
"eslint.config.mjs",
134+
"package.json",
135+
".github/workflows/ci.yml",
136+
".github/dependabot.yml",
137+
".commitlintrc.js",
138+
],
139+
});
140+
80141
const prettierResult = await env.runPrettier(appPath, true);
81142
expect(prettierResult.success).toBe(true);
82143

@@ -127,6 +188,18 @@ describe("Vite Integration Tests", () => {
127188
const output = await env.runSolvroConfig(appPath, ["--all", "--force"]);
128189
expect(output).toContain("Konfiguracja zakończona pomyślnie");
129190

191+
await expectGeneratedConfigToBeFormatted({
192+
env,
193+
appPath,
194+
expectedGeneratedFiles: [
195+
"eslint.config.js",
196+
"package.json",
197+
".github/workflows/ci.yml",
198+
".github/dependabot.yml",
199+
".commitlintrc.js",
200+
],
201+
});
202+
130203
const prettierResult = await env.runPrettier(appPath, true);
131204
expect(prettierResult.success).toBe(true);
132205

@@ -154,3 +227,55 @@ describe("Vite Integration Tests", () => {
154227
expect(packageJson).toContain('"prettier": "@solvro/config/prettier"');
155228
});
156229
});
230+
231+
describe("Commitlint Integration Tests", () => {
232+
let testEnv: TestEnvironment;
233+
const packageManager = getCurrentPackageManager();
234+
235+
beforeEach(async () => {
236+
testEnv = new TestEnvironment("commitlint-integration", packageManager);
237+
});
238+
239+
afterEach(() => {
240+
testEnv?.cleanup();
241+
});
242+
243+
test(`should allow valid commit message through husky commit-msg hook with ${packageManager.name}`, async () => {
244+
const env = testEnv;
245+
await env.setup();
246+
247+
const appPath = await env.createNextjsApp("commitlint-test-app");
248+
await env.installSolvroConfig(appPath);
249+
await env.initGitRepo(appPath);
250+
251+
const output = await env.runSolvroConfig(appPath, [
252+
"--commitlint",
253+
"--force",
254+
]);
255+
expect(output).toContain("Konfiguracja zakończona pomyślnie");
256+
257+
await expectGeneratedConfigToBeFormatted({
258+
env,
259+
appPath,
260+
expectedGeneratedFiles: ["package.json", ".commitlintrc.js"],
261+
});
262+
263+
const packageJson = JSON.parse(env.readFile(appPath, "package.json")) as {
264+
scripts?: Record<string, string>;
265+
};
266+
packageJson.scripts = packageJson.scripts ?? {};
267+
packageJson.scripts.test = packageJson.scripts.test ?? "true";
268+
(packageJson as { type?: string }).type = "module";
269+
env.writeFile(
270+
appPath,
271+
"package.json",
272+
JSON.stringify(packageJson, null, 2),
273+
);
274+
275+
await expectCommitlintHookToAllowMessage({
276+
env,
277+
appPath,
278+
message: "chore: test commit",
279+
});
280+
});
281+
});

tests/utils/test-environment.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,81 @@ export class TestEnvironment {
404404
}
405405
}
406406

407+
async runPrettierCheckIfInstalled(
408+
appPath: string,
409+
paths: string[] = ["."],
410+
): Promise<{ success: boolean; output: string; skipped: boolean }> {
411+
const packageJson = JSON.parse(this.readFile(appPath, "package.json")) as {
412+
dependencies?: Record<string, string>;
413+
devDependencies?: Record<string, string>;
414+
};
415+
const hasPrettier =
416+
packageJson.dependencies?.prettier != null ||
417+
packageJson.devDependencies?.prettier != null;
418+
419+
if (!hasPrettier) {
420+
return {
421+
success: true,
422+
output: "Skipped prettier check: prettier is not installed.",
423+
skipped: true,
424+
};
425+
}
426+
427+
if (paths.length === 0) {
428+
return {
429+
success: true,
430+
output: "Skipped prettier check: no files selected.",
431+
skipped: true,
432+
};
433+
}
434+
435+
try {
436+
const { stdout, stderr } = await this.execute({
437+
command: "localExecute",
438+
args: ["prettier", "--check", ...paths],
439+
cwd: appPath,
440+
label: "prettier-check",
441+
});
442+
return { success: true, output: stdout + stderr, skipped: false };
443+
} catch (error: any) {
444+
return {
445+
success: false,
446+
output: (error.stdout || "") + (error.stderr || ""),
447+
skipped: false,
448+
};
449+
}
450+
}
451+
452+
async commitWithMessage(
453+
appPath: string,
454+
message: string,
455+
): Promise<{ success: boolean; output: string }> {
456+
try {
457+
const { stdout: addStdout, stderr: addStderr } = await execWithLogging(
458+
"git",
459+
["add", "."],
460+
{ cwd: appPath },
461+
"git-add-for-commit",
462+
);
463+
const { stdout: commitStdout, stderr: commitStderr } =
464+
await execWithLogging(
465+
"git",
466+
["commit", "-m", message],
467+
{ cwd: appPath },
468+
"git-commit-with-message",
469+
);
470+
return {
471+
success: true,
472+
output: addStdout + addStderr + commitStdout + commitStderr,
473+
};
474+
} catch (error: any) {
475+
return {
476+
success: false,
477+
output: (error.stdout || "") + (error.stderr || ""),
478+
};
479+
}
480+
}
481+
407482
async buildNextjsApp(
408483
appPath: string,
409484
): Promise<{ success: boolean; output: string }> {

0 commit comments

Comments
 (0)