-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
getTestRule.js
163 lines (129 loc) · 4.1 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
'use strict';
const util = require('util');
const { basicChecks, lint } = require('stylelint');
function getTestRule(options = {}) {
return function testRule(schema) {
describe(`${schema.ruleName}`, () => {
const stylelintConfig = {
plugins: options.plugins || schema.plugins,
rules: {
[schema.ruleName]: schema.config,
},
};
let passingTestCases = schema.accept || [];
if (!schema.skipBasicChecks) {
passingTestCases = passingTestCases.concat(basicChecks);
}
setupTestCases({
name: 'accept',
cases: passingTestCases,
schema,
comparisons: (testCase) => async () => {
const stylelintOptions = {
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
};
const output = await lint(stylelintOptions);
expect(output.results[0].warnings).toEqual([]);
expect(output.results[0].parseErrors).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,
syntax: schema.syntax,
};
const outputAfterLint = await lint(stylelintOptions);
const actualWarnings = outputAfterLint.results[0].warnings;
expect(outputAfterLint.results[0].parseErrors).toEqual([]);
expect(actualWarnings).toHaveLength(testCase.warnings ? testCase.warnings.length : 1);
(testCase.warnings || [testCase]).forEach((expected, i) => {
const warning = actualWarnings[i];
expect(expected).toHaveMessage();
expect(warning.text).toBe(expected.message);
if (expected.line !== undefined) {
expect(warning.line).toBe(expected.line);
}
if (expected.column !== undefined) {
expect(warning.column).toBe(expected.column);
}
});
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,
});
expect(outputAfterLintOnFixedCode.results[0].warnings).toEqual(
outputAfterFix.results[0].warnings,
);
expect(outputAfterLintOnFixedCode.results[0].parseErrors).toEqual([]);
},
});
});
expect.extend({
toHaveMessage(testCase) {
if (testCase.message === undefined) {
return {
message: () => 'Expected "reject" test case to have a "message" property',
pass: false,
};
}
return {
pass: true,
};
},
});
};
}
function setupTestCases({ name, cases, schema, comparisons }) {
if (cases && cases.length) {
describe(`${name}`, () => {
cases.forEach((testCase) => {
if (testCase) {
const spec = testCase.only ? it.only : testCase.skip ? it.skip : it;
describe(`${util.inspect(schema.config)}`, () => {
describe(`${util.inspect(testCase.code)}`, () => {
spec(testCase.description || 'no description', comparisons(testCase));
});
});
}
});
});
}
}
function getOutputCss(output) {
const result = output.results[0]._postcssResult;
const css = result.root.toString(result.opts.syntax);
return css;
}
module.exports = getTestRule;