/
test.mjs
165 lines (141 loc) · 3.97 KB
/
test.mjs
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
import path from 'node:path';
import url from 'node:url';
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {loadRule} from '../../rules/utils/rule.js';
import SnapshotRuleTester from './snapshot-rule-tester.mjs';
import defaultParserOptions from './default-parser-options.mjs';
import parsers from './parsers.mjs';
function normalizeTests(tests) {
return tests.map(test => typeof test === 'string' ? {code: test} : test);
}
function normalizeInvalidTest(test, rule) {
const {code, output, errors} = test;
if (code === output) {
console.log(JSON.stringify(test, undefined, 2));
throw new Error('Remove output if your test do not fix code.');
}
if (Array.isArray(errors) && errors.some(error => error.suggestions) && rule.meta.hasSuggestions !== true) {
// This check will no longer be necessary if this change lands in ESLint 8: https://github.com/eslint/eslint/issues/14312
throw new Error('Rule with suggestion is missing `meta.hasSuggestions`.');
}
return {
// Use `null` instead of `code` to get a better message
// See https://github.com/eslint/eslint/blob/8a77b661bc921c3408bae01b3aa41579edfc6e58/lib/rule-tester/rule-tester.js#L847-L853
// eslint-disable-next-line unicorn/no-null
output: null,
...test,
};
}
function normalizeParser(options) {
const {
parser,
parserOptions,
} = options;
if (parser) {
if (parser.name) {
options.parser = parser.name;
}
if (parser.mergeParserOptions) {
options.parserOptions = parser.mergeParserOptions(parserOptions);
}
}
return options;
}
class Tester {
constructor(ruleId) {
this.ruleId = ruleId;
this.rule = loadRule(ruleId);
}
runTest(tests) {
const {beforeAll, testerOptions = {}, valid, invalid} = tests;
const tester = avaRuleTester(test, {
...testerOptions,
parserOptions: {
...defaultParserOptions,
...testerOptions.parserOptions,
},
});
if (beforeAll) {
beforeAll(tester);
}
return tester.run(
this.ruleId,
this.rule,
{
valid,
invalid: invalid.map(test => normalizeInvalidTest(test, this.rule)),
},
);
}
snapshot(tests) {
let {
testerOptions = {},
valid,
invalid,
} = tests;
testerOptions = normalizeParser(testerOptions);
valid = normalizeTests(valid).map(test => normalizeParser(test));
invalid = normalizeTests(invalid).map(test => normalizeParser(test));
const tester = new SnapshotRuleTester(test, {
...testerOptions,
parserOptions: {
...defaultParserOptions,
...testerOptions.parserOptions,
},
});
return tester.run(this.ruleId, this.rule, {valid, invalid});
}
}
function getTester(importMeta) {
const filename = url.fileURLToPath(importMeta.url);
const ruleId = path.basename(filename, '.mjs');
const tester = new Tester(ruleId);
const runTest = Tester.prototype.runTest.bind(tester);
runTest.snapshot = Tester.prototype.snapshot.bind(tester);
for (const [parserName, parserSettings] of Object.entries(parsers)) {
Reflect.defineProperty(runTest, parserName, {
value(tests) {
const testerOptions = tests.testerOptions || {};
const {parser, mergeParserOptions} = parserSettings;
return runTest({
...tests,
testerOptions: {
...testerOptions,
parser,
parserOptions: mergeParserOptions(testerOptions.parserOptions),
},
});
},
});
}
return {
ruleId,
rule: tester.rule,
test: runTest,
};
}
const addComment = (test, comment) => {
const {code, output} = test;
const fixedTest = {
...test,
code: `${code}\n/* ${comment} */`,
};
if (Object.prototype.hasOwnProperty.call(fixedTest, 'output') && typeof output === 'string') {
fixedTest.output = `${output}\n/* ${comment} */`;
}
return fixedTest;
};
const avoidTestTitleConflict = (tests, comment) => {
const {valid, invalid} = tests;
return {
...tests,
valid: normalizeTests(valid).map(test => addComment(test, comment)),
invalid: normalizeTests(invalid).map(test => addComment(test, comment)),
};
};
export {
getTester,
avoidTestTitleConflict,
parsers,
};