-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.test.ts
More file actions
95 lines (87 loc) · 3.4 KB
/
index.test.ts
File metadata and controls
95 lines (87 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {type RuleOutcome} from '@commitlint/types';
import test from 'ava';
import load19x from '@commitlint/load-19.x';
import lint19x from '@commitlint/lint-19.x';
import load20x from '@commitlint/load-20.x';
import lint20x from '@commitlint/lint-20.x';
import {rules} from './rules.js';
import * as plugin from './index.js';
test('exports rules', (t) => {
t.is(plugin.rules, rules);
});
/* eslint-disable @typescript-eslint/naming-convention */
const versions = {
'19.x': {load: load19x, lint: lint19x},
'20.x': {load: load20x, lint: lint20x},
} as const;
/* eslint-enable @typescript-eslint/naming-convention */
const loadPlugin = test.macro<[keyof typeof versions]>({
async exec(t, version) {
/**
* Specify the package file, which doesn't contain a configuration, to
* prevent `@commitlint/load` from searching the filesystem for an actual
* configuration.
*/
await t.notThrowsAsync(
versions[version].load({plugins: [plugin]}, {file: 'package.json'}),
);
},
title(_, version) {
return `@commitlint/load@${version} can load the plugin`;
},
});
test(loadPlugin, '19.x');
test(loadPlugin, '20.x');
/**
* The type of the `RulesConfig` have changed in [commitlint v20.3.1](
* https://github.com/conventional-changelog/commitlint/releases/tag/v20.3.1),
* and are no longer compatible with those from v19. Specifically the types of
* the `scope-case` and `scope-enum` rules have been changed to also accept a
* rule object. While the individual versions are able process this test without
* runtime or type errors, the combined signature causes type errors. To keep
* the test, a relatively long one, the same for both versions, the type of the
* rule config is cast.
*/
const lintUsingPluginRules = test.macro<[keyof typeof versions]>({
async exec(t, version) {
// eslint-disable-next-line @typescript-eslint/await-thenable
for await (const rule of Object.keys(plugin.rules)) {
const configs: Array<{async: boolean; ruleOutcome: RuleOutcome}> = [
{async: false, ruleOutcome: [true]},
{async: false, ruleOutcome: [false, `error message from ${rule}`]},
{async: true, ruleOutcome: [true]},
{async: true, ruleOutcome: [false, `error message from ${rule}`]},
];
// eslint-disable-next-line @typescript-eslint/await-thenable
for await (const config of configs) {
const report = await versions[version].lint(
'chore: basic commit message',
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
{
[rule]: [
2,
'always',
config.async
? async () => config.ruleOutcome
: () => config.ruleOutcome,
],
} as unknown as any,
{plugins: {'function-rules': plugin}},
);
const message = `rule "${rule}" can't be used by commitlint`;
t.is(report.valid, config.ruleOutcome[0], message);
if (config.ruleOutcome[1] !== undefined) {
t.is(report.warnings.length, 0, message);
t.is(report.errors.length, 1, message);
t.is(report.errors[0].name, rule, message);
t.is(report.errors[0].message, config.ruleOutcome[1], message);
}
}
}
},
title(_, version) {
return `@commitlint/lint@${version} can use the plugin rules`;
},
});
test(lintUsingPluginRules, '19.x');
test(lintUsingPluginRules, '20.x');