Skip to content

Commit 27e23e5

Browse files
authored
Refactor tests (#2036)
1 parent 55469e5 commit 27e23e5

File tree

5 files changed

+670
-228
lines changed

5 files changed

+670
-228
lines changed

scripts/internal-rules/fix-snapshot-test.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ const messages = {
1515
};
1616

1717
// Top-level `test.snapshot({invalid: []})`
18-
const selector = [
18+
const snapshotTestCallSelector = [
1919
'Program > ExpressionStatement.body > .expression',
2020
// `test.snapshot()`
2121
methodCallSelector({
2222
argumentsLength: 1,
2323
object: 'test',
2424
method: 'snapshot',
2525
}),
26+
].join('');
27+
28+
const propertySelector = [
29+
snapshotTestCallSelector,
2630
' > ObjectExpression.arguments:first-child',
2731
/*
2832
```
@@ -53,8 +57,7 @@ function * removeObjectProperty(node, fixer, sourceCode) {
5357
}
5458

5559
// The fix deletes lots of code, disabled auto-fix by default, unless `/* fix */ test.snapshot()` pattern is used.
56-
function hasFixMarkComment(propertyNode, sourceCode) {
57-
const snapshotTestCall = propertyNode.parent.parent.parent.parent.parent;
60+
function getFixMarkComment(snapshotTestCall, sourceCode) {
5861
assert.ok(snapshotTestCall.type === 'CallExpression');
5962
const comment = sourceCode.getTokenBefore(snapshotTestCall, {includeComments: true});
6063

@@ -66,7 +69,7 @@ function hasFixMarkComment(propertyNode, sourceCode) {
6669
|| comment.loc.start.line === snapshotTestCall.loc.start.line - 1
6770
)
6871
) {
69-
return true;
72+
return comment;
7073
}
7174
}
7275

@@ -75,14 +78,29 @@ module.exports = {
7578
const sourceCode = context.getSourceCode();
7679

7780
return {
78-
[selector](propertyNode) {
81+
[snapshotTestCallSelector](snapshotTestCall) {
82+
const comment = getFixMarkComment(snapshotTestCall, sourceCode);
83+
84+
if (!comment) {
85+
return;
86+
}
87+
88+
context.report({
89+
node: comment,
90+
messageId: MESSAGE_ID_REMOVE_FIX_MARK_COMMENT,
91+
});
92+
},
93+
[propertySelector](propertyNode) {
7994
const {key} = propertyNode;
8095

8196
switch (key.name) {
8297
case 'errors':
8398
case 'output': {
8499
const canFix = sourceCode.getCommentsInside(propertyNode).length === 0;
85-
const hasFixMark = hasFixMarkComment(propertyNode, sourceCode);
100+
const hasFixMark = Boolean(getFixMarkComment(
101+
propertyNode.parent.parent.parent.parent.parent,
102+
sourceCode,
103+
));
86104

87105
context.report({
88106
node: key,

test/new-for-builtins.mjs

+54-216
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@ import {getTester} from './utils/test.mjs';
55

66
const {test} = getTester(import.meta);
77

8-
const enforceNewError = builtin => ({
9-
message: `Use \`new ${builtin}()\` instead of \`${builtin}()\`.`,
10-
});
11-
12-
const disallowNewError = builtin => ({
13-
message: `Use \`${builtin}()\` instead of \`new ${builtin}()\`.`,
14-
});
15-
16-
test({
8+
test.snapshot({
179
valid: [
1810
'const foo = new Object()',
1911
'const foo = new Array()',
@@ -106,214 +98,7 @@ test({
10698
'const isObject = v => Object(v) === v;',
10799
'const isObject = v => globalThis.Object(v) === v;',
108100
'(x) !== Object(x)',
109-
],
110-
invalid: [
111-
{
112-
code: 'const foo = Object()',
113-
errors: [enforceNewError('Object')],
114-
output: 'const foo = new Object()',
115-
},
116-
{
117-
code: 'const foo = Array()',
118-
errors: [enforceNewError('Array')],
119-
output: 'const foo = new Array()',
120-
},
121-
{
122-
code: 'const foo = ArrayBuffer()',
123-
errors: [enforceNewError('ArrayBuffer')],
124-
output: 'const foo = new ArrayBuffer()',
125-
},
126-
{
127-
code: 'const foo = BigInt64Array()',
128-
errors: [enforceNewError('BigInt64Array')],
129-
output: 'const foo = new BigInt64Array()',
130-
},
131-
{
132-
code: 'const foo = BigUint64Array()',
133-
errors: [enforceNewError('BigUint64Array')],
134-
output: 'const foo = new BigUint64Array()',
135-
},
136-
{
137-
code: 'const foo = DataView()',
138-
errors: [enforceNewError('DataView')],
139-
output: 'const foo = new DataView()',
140-
},
141-
{
142-
code: 'const foo = Date()',
143-
errors: [enforceNewError('Date')],
144-
output: 'const foo = new Date()',
145-
},
146-
{
147-
code: 'const foo = Error()',
148-
errors: [enforceNewError('Error')],
149-
output: 'const foo = new Error()',
150-
},
151-
{
152-
code: 'const foo = Error(\'Foo bar\')',
153-
errors: [enforceNewError('Error')],
154-
output: 'const foo = new Error(\'Foo bar\')',
155-
},
156-
{
157-
code: 'const foo = Float32Array()',
158-
errors: [enforceNewError('Float32Array')],
159-
output: 'const foo = new Float32Array()',
160-
},
161-
{
162-
code: 'const foo = Float64Array()',
163-
errors: [enforceNewError('Float64Array')],
164-
output: 'const foo = new Float64Array()',
165-
},
166-
{
167-
code: 'const foo = Function()',
168-
errors: [enforceNewError('Function')],
169-
output: 'const foo = new Function()',
170-
},
171-
{
172-
code: 'const foo = Int8Array()',
173-
errors: [enforceNewError('Int8Array')],
174-
output: 'const foo = new Int8Array()',
175-
},
176-
{
177-
code: 'const foo = Int16Array()',
178-
errors: [enforceNewError('Int16Array')],
179-
output: 'const foo = new Int16Array()',
180-
},
181-
{
182-
code: 'const foo = Int32Array()',
183-
errors: [enforceNewError('Int32Array')],
184-
output: 'const foo = new Int32Array()',
185-
},
186-
{
187-
code: 'const foo = (( Map ))()',
188-
errors: [enforceNewError('Map')],
189-
output: 'const foo = new (( Map ))()',
190-
},
191-
{
192-
code: 'const foo = Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])',
193-
errors: [enforceNewError('Map')],
194-
output: 'const foo = new Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])',
195-
},
196-
{
197-
code: 'const foo = WeakMap()',
198-
errors: [enforceNewError('WeakMap')],
199-
output: 'const foo = new WeakMap()',
200-
},
201-
{
202-
code: 'const foo = Set()',
203-
errors: [enforceNewError('Set')],
204-
output: 'const foo = new Set()',
205-
},
206-
{
207-
code: 'const foo = WeakSet()',
208-
errors: [enforceNewError('WeakSet')],
209-
output: 'const foo = new WeakSet()',
210-
},
211-
{
212-
code: 'const foo = Promise()',
213-
errors: [enforceNewError('Promise')],
214-
output: 'const foo = new Promise()',
215-
},
216-
{
217-
code: 'const foo = RegExp()',
218-
errors: [enforceNewError('RegExp')],
219-
output: 'const foo = new RegExp()',
220-
},
221-
{
222-
code: 'const foo = Uint8Array()',
223-
errors: [enforceNewError('Uint8Array')],
224-
output: 'const foo = new Uint8Array()',
225-
},
226-
{
227-
code: 'const foo = Uint16Array()',
228-
errors: [enforceNewError('Uint16Array')],
229-
output: 'const foo = new Uint16Array()',
230-
},
231-
{
232-
code: 'const foo = Uint32Array()',
233-
errors: [enforceNewError('Uint32Array')],
234-
output: 'const foo = new Uint32Array()',
235-
},
236-
{
237-
code: 'const foo = Uint8ClampedArray()',
238-
errors: [enforceNewError('Uint8ClampedArray')],
239-
output: 'const foo = new Uint8ClampedArray()',
240-
},
241-
{
242-
code: 'const foo = new BigInt(123)',
243-
errors: [disallowNewError('BigInt')],
244-
output: 'const foo = BigInt(123)',
245-
},
246-
{
247-
code: 'const foo = new Boolean()',
248-
errors: [disallowNewError('Boolean')],
249-
},
250-
{
251-
code: 'const foo = new Number()',
252-
errors: [disallowNewError('Number')],
253-
},
254-
{
255-
code: 'const foo = new Number(\'123\')',
256-
errors: [disallowNewError('Number')],
257-
},
258-
{
259-
code: 'const foo = new String()',
260-
errors: [disallowNewError('String')],
261-
},
262-
{
263-
code: 'const foo = new Symbol()',
264-
errors: [disallowNewError('Symbol')],
265-
output: 'const foo = Symbol()',
266-
},
267-
{
268-
code: `
269-
function varCheck() {
270-
{
271-
var WeakMap = function() {};
272-
}
273-
// This should not reported
274-
return WeakMap()
275-
}
276-
function constCheck() {
277-
{
278-
const Array = function() {};
279-
}
280-
return Array()
281-
}
282-
function letCheck() {
283-
{
284-
let Map = function() {};
285-
}
286-
return Map()
287-
}
288-
`,
289-
errors: [enforceNewError('Array'), enforceNewError('Map')],
290-
output: `
291-
function varCheck() {
292-
{
293-
var WeakMap = function() {};
294-
}
295-
// This should not reported
296-
return WeakMap()
297-
}
298-
function constCheck() {
299-
{
300-
const Array = function() {};
301-
}
302-
return new Array()
303-
}
304-
function letCheck() {
305-
{
306-
let Map = function() {};
307-
}
308-
return new Map()
309-
}
310-
`,
311-
},
312-
],
313-
});
314101

315-
test.snapshot({
316-
valid: [
317102
{
318103
code: 'new Symbol("")',
319104
globals: {Symbol: 'off'},
@@ -414,5 +199,58 @@ test.snapshot({
414199
`,
415200
globals: {Array: 'off'},
416201
},
202+
'const foo = Object()',
203+
'const foo = Array()',
204+
'const foo = ArrayBuffer()',
205+
'const foo = BigInt64Array()',
206+
'const foo = BigUint64Array()',
207+
'const foo = DataView()',
208+
'const foo = Date()',
209+
'const foo = Error()',
210+
'const foo = Error(\'Foo bar\')',
211+
'const foo = Float32Array()',
212+
'const foo = Float64Array()',
213+
'const foo = Function()',
214+
'const foo = Int8Array()',
215+
'const foo = Int16Array()',
216+
'const foo = Int32Array()',
217+
'const foo = (( Map ))()',
218+
'const foo = Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])',
219+
'const foo = WeakMap()',
220+
'const foo = Set()',
221+
'const foo = WeakSet()',
222+
'const foo = Promise()',
223+
'const foo = RegExp()',
224+
'const foo = Uint8Array()',
225+
'const foo = Uint16Array()',
226+
'const foo = Uint32Array()',
227+
'const foo = Uint8ClampedArray()',
228+
'const foo = new BigInt(123)',
229+
'const foo = new Boolean()',
230+
'const foo = new Number()',
231+
'const foo = new Number(\'123\')',
232+
'const foo = new String()',
233+
'const foo = new Symbol()',
234+
`
235+
function varCheck() {
236+
{
237+
var WeakMap = function() {};
238+
}
239+
// This should not reported
240+
return WeakMap()
241+
}
242+
function constCheck() {
243+
{
244+
const Array = function() {};
245+
}
246+
return Array()
247+
}
248+
function letCheck() {
249+
{
250+
let Map = function() {};
251+
}
252+
return Map()
253+
}
254+
`,
417255
],
418256
});

test/no-lonely-if.mjs

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {getTester} from './utils/test.mjs';
33

44
const {test} = getTester(import.meta);
55

6-
test({
6+
test.snapshot({
77
valid: [
88
outdent`
99
if (a) {
@@ -30,11 +30,6 @@ test({
3030
}
3131
`,
3232
],
33-
invalid: [],
34-
});
35-
36-
test.snapshot({
37-
valid: [],
3833
invalid: [
3934
outdent`
4035
if (a) {

0 commit comments

Comments
 (0)