|
1 | 1 | import { test, expect } from "vitest"; |
| 2 | +import type { Parser } from "@commitlint/types"; |
2 | 3 | import { RuleConfigSeverity } from "@commitlint/types"; |
3 | 4 |
|
4 | 5 | import lint from "./lint.js"; |
@@ -308,3 +309,81 @@ test("passes for async rule", async () => { |
308 | 309 |
|
309 | 310 | expect(report.valid).toBe(true); |
310 | 311 | }); |
| 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