|
| 1 | +/** |
| 2 | + * @file Custom Matchers - ruleOutcome |
| 3 | + * @module tests/setup/matchers/ruleOutcome |
| 4 | + */ |
| 5 | + |
| 6 | +import type cl from '@commitlint/types' |
| 7 | +import type { MatcherState, SyncExpectationResult } from '@vitest/expect' |
| 8 | + |
| 9 | +/** |
| 10 | + * Expects an array of {@linkcode cl.RuleOutcome} objects to include a specific |
| 11 | + * {@linkcode cl.LintRuleOutcome}. |
| 12 | + * |
| 13 | + * @this {MatcherState} |
| 14 | + * |
| 15 | + * @param {MatcherState} this - Matcher state |
| 16 | + * @param {unknown} received - Received value |
| 17 | + * @param {string} name - Name of expected {@linkcode cl.LintRuleOutcome} |
| 18 | + * @param {0 | 1 | 2} level - Expected {@linkcode cl.RuleConfigSeverity} |
| 19 | + * @return {SyncExpectationResult} Expectation result |
| 20 | + */ |
| 21 | +function ruleOutcome( |
| 22 | + this: MatcherState, |
| 23 | + received: unknown, |
| 24 | + name: string, |
| 25 | + level: 0 | 1 | 2 |
| 26 | +): SyncExpectationResult { |
| 27 | + /** |
| 28 | + * Constructs an expectation result message. |
| 29 | + * |
| 30 | + * @return {string} Expectation result message |
| 31 | + */ |
| 32 | + const message = (): string => { |
| 33 | + /** |
| 34 | + * Stringified version of {@linkcode received}. |
| 35 | + * |
| 36 | + * @const {string} rs |
| 37 | + */ |
| 38 | + const rs: string = `${this.utils.printReceived(received)}` |
| 39 | + |
| 40 | + return `expected ${rs} to have rule outcome "${name}" of severity ${level}` |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Expected values. |
| 45 | + * |
| 46 | + * @const {[Pick<cl.LintRuleOutcome, 'name' | 'level'>]} expected |
| 47 | + */ |
| 48 | + const expected: [Pick<cl.LintRuleOutcome, 'level' | 'name'>] = [ |
| 49 | + { level: level, name } |
| 50 | + ] |
| 51 | + |
| 52 | + return Array.isArray(received) |
| 53 | + ? { |
| 54 | + actual: received, |
| 55 | + expected, |
| 56 | + message, |
| 57 | + pass: received.find((outcome: cl.LintRuleOutcome): boolean => { |
| 58 | + return outcome.name === name && outcome.level === level |
| 59 | + }) |
| 60 | + } |
| 61 | + : { actual: received, expected, message, pass: false } |
| 62 | +} |
| 63 | + |
| 64 | +expect.extend({ ruleOutcome }) |
0 commit comments