Skip to content

Commit 496be4d

Browse files
committed
feat(test): addes a test confirming functionality doesn't exist
This adds tests to confirm that functionality for #4816 doesn't exist and doesn't work
1 parent 9cd6a50 commit 496be4d

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

@commitlint/lint/src/lint.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { test, expect } from "vitest";
2+
import type { Parser } from "@commitlint/types";
23
import { RuleConfigSeverity } from "@commitlint/types";
34

45
import lint from "./lint.js";
@@ -308,3 +309,81 @@ test("passes for async rule", async () => {
308309

309310
expect(report.valid).toBe(true);
310311
});
312+
313+
test("uses custom parser when provided", async () => {
314+
const customParser: Parser = (message) => ({
315+
type: "feat",
316+
scope: null,
317+
subject: "my-feature",
318+
body: null,
319+
footer: null,
320+
header: message.trim(),
321+
});
322+
323+
const report = await lint(
324+
"any message",
325+
{
326+
"type-enum": [RuleConfigSeverity.Error, "always", ["feat"]],
327+
},
328+
{ parser: customParser },
329+
);
330+
331+
expect(report.valid).toBe(true);
332+
expect(report.errors.length).toBe(0);
333+
});
334+
335+
test("custom parser output is used by rules", async () => {
336+
const customParser: Parser = (_message) => ({
337+
type: null,
338+
scope: "auth",
339+
subject: "fix login",
340+
body: null,
341+
footer: null,
342+
header: "scope(subject): description",
343+
});
344+
345+
const report = await lint(
346+
"any message",
347+
{
348+
"type-empty": [RuleConfigSeverity.Error, "never"],
349+
},
350+
{ parser: customParser },
351+
);
352+
353+
expect(report.valid).toBe(false);
354+
expect(report.errors.length).toBe(1);
355+
});
356+
357+
test("custom parser receives parser options as second argument", async () => {
358+
let receivedOpts: Record<string, unknown> | undefined;
359+
360+
const customParser: Parser = (_message, opts) => {
361+
receivedOpts =
362+
typeof opts === "object" && opts !== null ? (opts as Record<string, unknown>) : {};
363+
return {
364+
header: "custom-parsed-header",
365+
type: "chore",
366+
subject: null,
367+
body: null,
368+
footer: null,
369+
merge: null,
370+
revert: null,
371+
notes: [],
372+
mentions: [],
373+
references: [],
374+
} as unknown as ReturnType<Parser>;
375+
};
376+
377+
const customOpts = { headerPattern: /^custom-parsed-header/ };
378+
379+
await lint(
380+
"any message",
381+
{
382+
"type-enum": [RuleConfigSeverity.Error, "always", ["chore"]],
383+
},
384+
{ parser: customParser, parserOpts: customOpts },
385+
);
386+
387+
expect(receivedOpts).toBeDefined();
388+
expect((receivedOpts as Record<string, unknown>).headerPattern).toBeDefined();
389+
});

0 commit comments

Comments
 (0)