-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetTestRuleConfigs.js
71 lines (60 loc) · 1.99 KB
/
getTestRuleConfigs.js
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
'use strict';
const { inspect } = require('node:util');
/** @type {import('.').getTestRuleConfigs} */
module.exports = function getTestRuleConfigs(options = {}) {
return function testRuleConfigs({
ruleName,
accept = [],
reject = [],
only = false,
skip = false,
plugins = options.plugins,
loadLint: schemaLoadLint,
}) {
if (accept.length === 0 && reject.length === 0) {
throw new TypeError('The either "accept" or "reject" property must not be empty');
}
const loadLint =
schemaLoadLint || options.loadLint || (() => import('stylelint').then((m) => m.default.lint)); // eslint-disable-line n/no-unpublished-import -- Avoid auto-install of `stylelint` peer dependency.
/** @type {import('stylelint').PublicApi['lint']} */
let lint;
beforeAll(async () => {
lint = await loadLint();
});
const testGroup = only ? describe.only : skip ? describe.skip : describe;
testGroup(`${ruleName} configs`, () => {
/**
* @param {import('.').ConfigCase} case
* @param {(warnings: unknown[]) => void} comparison
*/
function testConfig({ config, description, only: onlyTest, skip: skipTest }, comparison) {
const testFn = onlyTest ? test.only : skipTest ? test.skip : test;
testFn(`${description || inspect(config)}`, async () => {
const lintConfig = {
plugins,
rules: { [ruleName]: config },
};
const { results } = await lint({ code: '', config: lintConfig });
expect(results).toHaveLength(1);
comparison(results[0].invalidOptionWarnings);
});
}
describe('accept', () => {
accept.forEach((c) => {
testConfig(c, (warnings) => {
// eslint-disable-next-line jest/no-standalone-expect
expect(warnings).toEqual([]);
});
});
});
describe('reject', () => {
reject.forEach((c) => {
testConfig(c, (warnings) => {
// eslint-disable-next-line jest/no-standalone-expect
expect(warnings).toEqual([{ text: expect.stringMatching(`"${ruleName}"`) }]);
});
});
});
});
};
};