|
| 1 | +import * as plugin from '../src'; |
| 2 | +import { Plugin, RuleOutcome, RulesConfig } from '@commitlint/types'; |
| 3 | +import { describe, expect, it } from '@jest/globals'; |
| 4 | +import lint from '@commitlint/lint'; |
| 5 | +import load from '@commitlint/load'; |
| 6 | + |
| 7 | +describe('commitlint plugin function rules', () => { |
| 8 | + it('provides rules that can be used with commitlint', async () => { |
| 9 | + for await (const rule of Object.keys(plugin.rules)) { |
| 10 | + const results: RuleOutcome[] = [ |
| 11 | + [true], |
| 12 | + [false, `error message from ${rule}`], |
| 13 | + ]; |
| 14 | + for await (const result of results) { |
| 15 | + const rules: Partial<RulesConfig> = {}; |
| 16 | + rules[rule] = [ |
| 17 | + 2, |
| 18 | + 'always', |
| 19 | + () => { |
| 20 | + return result; |
| 21 | + }, |
| 22 | + ]; |
| 23 | + |
| 24 | + const config = await load({ |
| 25 | + plugins: [plugin as Plugin], |
| 26 | + rules, |
| 27 | + }); |
| 28 | + |
| 29 | + const report = await lint('chore: basic commit message', config.rules, { |
| 30 | + plugins: { |
| 31 | + 'function-rules': plugin as Plugin, |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(report.valid).toEqual(result[0]); |
| 36 | + if (result[1] !== undefined) { |
| 37 | + expect(report.warnings).toHaveLength(0); |
| 38 | + expect(report.errors).toHaveLength(1); |
| 39 | + expect(report.errors[0].name).toEqual(rule); |
| 40 | + expect(report.errors[0].message).toEqual(result[1]); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | +}); |
0 commit comments