Skip to content

Commit 245f921

Browse files
committed
test(commitlint): add negative case
1 parent bcee512 commit 245f921

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/cli/install-commitlint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const packageJson = new PackageJson();
1313
export const installCommitLint = async () => {
1414
await installHusky();
1515

16-
await packageJson.install("@commitlint/cli", { version: "20", dev: true });
16+
await packageJson.install("@commitlint/cli", { version: "^20", dev: true });
1717

1818
await writeFile(
1919
path.join(root, ".husky/commit-msg"),

tests/integration.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const expectGeneratedConfigToBeFormatted = async ({
1919
appPath,
2020
generatedFilesThatExist,
2121
);
22+
expect(prettierCheckResult.skipped, prettierCheckResult.output).toBe(false);
2223
expect(prettierCheckResult.success, prettierCheckResult.output).toBe(true);
2324
};
2425

@@ -40,6 +41,30 @@ const expectCommitlintHookToAllowMessage = async ({
4041
expect(commitResult.success, commitResult.output).toBe(true);
4142
};
4243

44+
const expectCommitlintHookToRejectMessage = async ({
45+
env,
46+
appPath,
47+
message,
48+
}: {
49+
env: TestEnvironment;
50+
appPath: string;
51+
message: string;
52+
}) => {
53+
env.writeFile(
54+
appPath,
55+
"commitlint-hook-test-invalid.txt",
56+
`commitlint hook invalid test ${Date.now()}\n`,
57+
);
58+
const commitResult = await env.commitWithMessage(appPath, message);
59+
expect(commitResult.success, commitResult.output).toBe(false);
60+
expect(
61+
/subject-empty|type-empty|commit-msg script failed/i.test(
62+
commitResult.output,
63+
),
64+
commitResult.output,
65+
).toBe(true);
66+
};
67+
4368
describe("Next.js Integration Tests", () => {
4469
let testEnv: TestEnvironment;
4570
const packageManager = getCurrentPackageManager();
@@ -265,7 +290,6 @@ describe("Commitlint Integration Tests", () => {
265290
};
266291
packageJson.scripts = packageJson.scripts ?? {};
267292
packageJson.scripts.test = packageJson.scripts.test ?? "true";
268-
(packageJson as { type?: string }).type = "module";
269293
env.writeFile(
270294
appPath,
271295
"package.json",
@@ -277,5 +301,11 @@ describe("Commitlint Integration Tests", () => {
277301
appPath,
278302
message: "chore: test commit",
279303
});
304+
305+
await expectCommitlintHookToRejectMessage({
306+
env,
307+
appPath,
308+
message: "invalid commit message",
309+
});
280310
});
281311
});

tests/utils/test-environment.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,6 @@ export class TestEnvironment {
416416
packageJson.dependencies?.prettier != null ||
417417
packageJson.devDependencies?.prettier != null;
418418

419-
if (!hasPrettier) {
420-
return {
421-
success: true,
422-
output: "Skipped prettier check: prettier is not installed.",
423-
skipped: true,
424-
};
425-
}
426-
427419
if (paths.length === 0) {
428420
return {
429421
success: true,
@@ -432,12 +424,20 @@ export class TestEnvironment {
432424
};
433425
}
434426

427+
const prettierCommand: keyof PackageManagerConfig = hasPrettier
428+
? "localExecute"
429+
: "downloadExecute";
430+
431+
const prettierArgs = hasPrettier
432+
? ["prettier", "--check", ...paths]
433+
: ["prettier", "--check", ...paths];
434+
435435
try {
436436
const { stdout, stderr } = await this.execute({
437-
command: "localExecute",
438-
args: ["prettier", "--check", ...paths],
437+
command: prettierCommand,
438+
args: prettierArgs,
439439
cwd: appPath,
440-
label: "prettier-check",
440+
label: hasPrettier ? "prettier-check" : "prettier-check-dlx",
441441
});
442442
return { success: true, output: stdout + stderr, skipped: false };
443443
} catch (error: any) {

0 commit comments

Comments
 (0)