-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
getTestRule.js
205 lines (169 loc) · 5.38 KB
/
getTestRule.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
const { inspect } = require('node:util');
/**
* @typedef {import('.').TestCase} TestCase
* @typedef {import('.').TestSchema} TestSchema
*/
/** @type {import('.').getTestRule} */
module.exports = function getTestRule(options = {}) {
return function testRule(schema) {
const loadLint =
schema.loadLint ||
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();
});
describe(`${schema.ruleName}`, () => {
const stylelintConfig = {
plugins: options.plugins || schema.plugins,
rules: {
[schema.ruleName]: schema.config,
},
};
setupTestCases({
name: 'accept',
cases: schema.accept,
schema,
comparisons: (testCase) => async () => {
const stylelintOptions = {
code: testCase.code,
config: stylelintConfig,
customSyntax: schema.customSyntax,
codeFilename: testCase.codeFilename || schema.codeFilename,
};
const output = await lint(stylelintOptions);
expect(output.results[0].warnings).toEqual([]);
expect(output.results[0].parseErrors).toEqual([]);
expect(output.results[0].invalidOptionWarnings).toEqual([]);
if (!schema.fix) return;
// Check that --fix doesn't change code
const outputAfterFix = await lint({ ...stylelintOptions, fix: true });
const fixedCode = getOutputCss(outputAfterFix);
expect(fixedCode).toBe(testCase.code);
},
});
setupTestCases({
name: 'reject',
cases: schema.reject,
schema,
comparisons: (testCase) => async () => {
const stylelintOptions = {
code: testCase.code,
config: stylelintConfig,
customSyntax: schema.customSyntax,
codeFilename: testCase.codeFilename || schema.codeFilename,
};
const outputAfterLint = await lint(stylelintOptions);
const actualWarnings = [
...outputAfterLint.results[0].invalidOptionWarnings,
...outputAfterLint.results[0].warnings,
];
expect(outputAfterLint.results[0]).toMatchObject({ parseErrors: [] });
expect(actualWarnings).toHaveLength(testCase.warnings ? testCase.warnings.length : 1);
for (const [i, expected] of (testCase.warnings || [testCase]).entries()) {
// @ts-expect-error -- This is our custom matcher.
expect(expected).toHaveMessage();
const expectedWarning = {
text: expected.message,
line: expected.line,
column: expected.column,
endLine: expected.endLine,
endColumn: expected.endColumn,
};
for (const [key, value] of Object.entries(expectedWarning)) {
if (value === undefined) {
// @ts-expect-error -- Allow a partial object.
delete expectedWarning[key];
}
}
expect(actualWarnings[i]).toMatchObject(expectedWarning);
}
if (!schema.fix) return;
// Check that --fix doesn't change code
if (schema.fix && !testCase.fixed && testCase.fixed !== '' && !testCase.unfixable) {
throw new Error(
'If using { fix: true } in test schema, all reject cases must have { fixed: .. }',
);
}
const outputAfterFix = await lint({ ...stylelintOptions, fix: true });
const fixedCode = getOutputCss(outputAfterFix);
if (!testCase.unfixable) {
expect(fixedCode).toBe(testCase.fixed);
expect(fixedCode).not.toBe(testCase.code);
} else {
// can't fix
if (testCase.fixed) {
expect(fixedCode).toBe(testCase.fixed);
}
expect(fixedCode).toBe(testCase.code);
}
// Checks whether only errors other than those fixed are reported
const outputAfterLintOnFixedCode = await lint({
...stylelintOptions,
code: fixedCode,
fix: testCase.unfixable,
});
expect(outputAfterLintOnFixedCode.results[0]).toMatchObject({
warnings: outputAfterFix.results[0].warnings,
parseErrors: [],
});
},
});
});
expect.extend({
toHaveMessage(testCase) {
if (testCase.message === undefined) {
return {
message: () => 'Expected "reject" test case to have a "message" property',
pass: false,
};
}
return {
message: () => '',
pass: true,
};
},
});
};
};
/**
* @template {TestCase} T
* @param {{
* name: string,
* cases: T[] | undefined,
* schema: TestSchema,
* comparisons: (testCase: T) => jest.ProvidesCallback,
* }} args
* @returns {void}
*/
function setupTestCases({ name, cases, schema, comparisons }) {
if (cases && cases.length) {
const testGroup = schema.only ? describe.only : schema.skip ? describe.skip : describe;
testGroup(`${name}`, () => {
cases.forEach((testCase) => {
if (testCase) {
const spec = testCase.only ? it.only : testCase.skip ? it.skip : it;
describe(`${inspect(schema.config)}`, () => {
describe(`${inspect(testCase.code)}`, () => {
spec(testCase.description || 'no description', comparisons(testCase));
});
});
}
});
});
}
}
/**
* @param {import('stylelint').LinterResult} output
* @returns {string}
*/
function getOutputCss(output) {
const result = output.results[0]._postcssResult;
if (result && result.root && result.opts) {
return result.root.toString(result.opts.syntax);
}
throw new TypeError('Invalid result');
}