Skip to content

Commit 040c605

Browse files
authored
fix: improve handling of .each calls and with tagged literals (#814)
* fix: improve handling of `.each` calls and with tagged literals * refactor(no-focused-tests): greatly deduplicate code * refactor: remove unneeded utils * refactor(no-standalone-expect): remove unneeded check * test(no-conditional-expect): add cases for `each()()` * test(no-test-return-statement): add cases for `each()()` * fix(no-if): support `each()()`
1 parent 6536594 commit 040c605

19 files changed

+280
-269
lines changed

src/rules/__tests__/lowercase-name.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ ruleTester.run('lowercase-name', rule, {
191191
{
192192
messageId: 'unexpectedLowercase',
193193
data: { method: TestCaseName.it },
194-
column: 9,
194+
column: 29,
195195
line: 1,
196196
},
197197
],
@@ -203,7 +203,7 @@ ruleTester.run('lowercase-name', rule, {
203203
{
204204
messageId: 'unexpectedLowercase',
205205
data: { method: DescribeAlias.describe },
206-
column: 15,
206+
column: 35,
207207
line: 1,
208208
},
209209
],

src/rules/__tests__/no-conditional-expect.test.ts

+65
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ ruleTester.run('logical conditions', rule, {
151151
`,
152152
errors: [{ messageId: 'conditionalExpect' }],
153153
},
154+
{
155+
code: `
156+
it.each()('foo', () => {
157+
something || expect(something).toHaveBeenCalled();
158+
})
159+
`,
160+
errors: [{ messageId: 'conditionalExpect' }],
161+
},
154162
{
155163
code: `
156164
function getValue() {
@@ -218,6 +226,14 @@ ruleTester.run('conditional conditions', rule, {
218226
`,
219227
errors: [{ messageId: 'conditionalExpect' }],
220228
},
229+
{
230+
code: `
231+
it.each()('foo', () => {
232+
something ? noop() : expect(something).toHaveBeenCalled();
233+
})
234+
`,
235+
errors: [{ messageId: 'conditionalExpect' }],
236+
},
221237
{
222238
code: `
223239
function getValue() {
@@ -304,6 +320,19 @@ ruleTester.run('switch conditions', rule, {
304320
`,
305321
errors: [{ messageId: 'conditionalExpect' }],
306322
},
323+
{
324+
code: `
325+
it.each()('foo', () => {
326+
switch(something) {
327+
case 'value':
328+
expect(something).toHaveBeenCalled();
329+
default:
330+
break;
331+
}
332+
})
333+
`,
334+
errors: [{ messageId: 'conditionalExpect' }],
335+
},
307336
{
308337
code: `
309338
function getValue() {
@@ -384,6 +413,18 @@ ruleTester.run('if conditions', rule, {
384413
`,
385414
errors: [{ messageId: 'conditionalExpect' }],
386415
},
416+
{
417+
code: `
418+
it.each()('foo', () => {
419+
if(!doSomething) {
420+
// do nothing
421+
} else {
422+
expect(something).toHaveBeenCalled();
423+
}
424+
})
425+
`,
426+
errors: [{ messageId: 'conditionalExpect' }],
427+
},
387428
{
388429
code: `
389430
function getValue() {
@@ -491,6 +532,18 @@ ruleTester.run('catch conditions', rule, {
491532
`,
492533
errors: [{ messageId: 'conditionalExpect' }],
493534
},
535+
{
536+
code: `
537+
it.each()('foo', () => {
538+
try {
539+
540+
} catch (err) {
541+
expect(err).toMatch('Error');
542+
}
543+
})
544+
`,
545+
errors: [{ messageId: 'conditionalExpect' }],
546+
},
494547
{
495548
code: `
496549
it.skip.each\`\`('foo', () => {
@@ -503,6 +556,18 @@ ruleTester.run('catch conditions', rule, {
503556
`,
504557
errors: [{ messageId: 'conditionalExpect' }],
505558
},
559+
{
560+
code: `
561+
it.skip.each()('foo', () => {
562+
try {
563+
564+
} catch (err) {
565+
expect(err).toMatch('Error');
566+
}
567+
})
568+
`,
569+
errors: [{ messageId: 'conditionalExpect' }],
570+
},
506571
{
507572
code: `
508573
function getValue() {

src/rules/__tests__/no-focused-tests.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ruleTester.run('no-focused-tests', rule, {
4646
],
4747
},
4848
{
49-
code: 'describe.only.each()',
49+
code: 'describe.only.each()()',
5050
errors: [
5151
{
5252
messageId: 'focusedTest',
@@ -55,7 +55,7 @@ ruleTester.run('no-focused-tests', rule, {
5555
suggestions: [
5656
{
5757
messageId: 'suggestRemoveFocus',
58-
output: 'describe.each()',
58+
output: 'describe.each()()',
5959
},
6060
],
6161
},
@@ -126,7 +126,7 @@ ruleTester.run('no-focused-tests', rule, {
126126
],
127127
},
128128
{
129-
code: 'it.only.each()',
129+
code: 'it.only.each()()',
130130
errors: [
131131
{
132132
messageId: 'focusedTest',
@@ -135,7 +135,7 @@ ruleTester.run('no-focused-tests', rule, {
135135
suggestions: [
136136
{
137137
messageId: 'suggestRemoveFocus',
138-
output: 'it.each()',
138+
output: 'it.each()()',
139139
},
140140
],
141141
},
@@ -206,7 +206,7 @@ ruleTester.run('no-focused-tests', rule, {
206206
],
207207
},
208208
{
209-
code: 'test.only.each()',
209+
code: 'test.only.each()()',
210210
errors: [
211211
{
212212
messageId: 'focusedTest',
@@ -215,7 +215,7 @@ ruleTester.run('no-focused-tests', rule, {
215215
suggestions: [
216216
{
217217
messageId: 'suggestRemoveFocus',
218-
output: 'test.each()',
218+
output: 'test.each()()',
219219
},
220220
],
221221
},
@@ -286,7 +286,7 @@ ruleTester.run('no-focused-tests', rule, {
286286
],
287287
},
288288
{
289-
code: 'fit.each()',
289+
code: 'fit.each()()',
290290
errors: [
291291
{
292292
messageId: 'focusedTest',
@@ -295,7 +295,7 @@ ruleTester.run('no-focused-tests', rule, {
295295
suggestions: [
296296
{
297297
messageId: 'suggestRemoveFocus',
298-
output: 'it.each()',
298+
output: 'it.each()()',
299299
},
300300
],
301301
},

src/rules/__tests__/no-if.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ ruleTester.run('conditional expressions', rule, {
2727
};
2828
});
2929
`,
30+
dedent`
31+
it.each()('foo', function () {
32+
const foo = function (bar) {
33+
return foo ? bar : null;
34+
};
35+
});
36+
`,
3037
],
3138
invalid: [
3239
{
@@ -120,6 +127,11 @@ ruleTester.run('switch statements', rule, {
120127
switch('bar') {}
121128
})
122129
`,
130+
dedent`
131+
describe.skip.each()('foo', () => {
132+
switch('bar') {}
133+
})
134+
`,
123135
dedent`
124136
xdescribe('foo', () => {
125137
switch('bar') {}
@@ -501,6 +513,13 @@ ruleTester.run('if statements', rule, {
501513
});
502514
})
503515
`,
516+
dedent`
517+
describe.each\`\`('foo', () => {
518+
afterEach(() => {
519+
if('bar') {}
520+
});
521+
})
522+
`,
504523
dedent`
505524
describe('foo', () => {
506525
beforeEach(() => {
@@ -826,6 +845,20 @@ ruleTester.run('if statements', rule, {
826845
},
827846
],
828847
},
848+
{
849+
code: dedent`
850+
it.each()('foo', () => {
851+
callExpression()
852+
if ('bar') {}
853+
})
854+
`,
855+
errors: [
856+
{
857+
data: { condition: 'if' },
858+
messageId: 'conditionalInTest',
859+
},
860+
],
861+
},
829862
{
830863
code: dedent`
831864
it.only.each\`\`('foo', () => {
@@ -840,6 +873,20 @@ ruleTester.run('if statements', rule, {
840873
},
841874
],
842875
},
876+
{
877+
code: dedent`
878+
it.only.each()('foo', () => {
879+
callExpression()
880+
if ('bar') {}
881+
})
882+
`,
883+
errors: [
884+
{
885+
data: { condition: 'if' },
886+
messageId: 'conditionalInTest',
887+
},
888+
],
889+
},
843890
{
844891
code: dedent`
845892
describe('valid', () => {

src/rules/__tests__/no-test-return-statement.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ ruleTester.run('no-test-return-statement', rule, {
6868
`,
6969
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
7070
},
71+
{
72+
code: dedent`
73+
it.each()("one", function () {
74+
return expect(1).toBe(1);
75+
});
76+
`,
77+
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
78+
},
7179
{
7280
code: dedent`
7381
it.only.each\`\`("one", function () {
@@ -76,6 +84,14 @@ ruleTester.run('no-test-return-statement', rule, {
7684
`,
7785
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
7886
},
87+
{
88+
code: dedent`
89+
it.only.each()("one", function () {
90+
return expect(1).toBe(1);
91+
});
92+
`,
93+
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
94+
},
7995
{
8096
code: dedent`
8197
it("one", myTest);

src/rules/__tests__/utils.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const rule = createRule({
2020
messages: {
2121
details: [
2222
'callType', //
23+
'numOfArgs',
2324
]
2425
.map(data => `${data}: {{ ${data} }}`)
2526
.join('\n'),
@@ -38,7 +39,10 @@ const rule = createRule({
3839
context.report({
3940
messageId: 'details',
4041
node,
41-
data: { callType },
42+
data: {
43+
callType,
44+
numOfArgs: node.arguments.length,
45+
},
4246
});
4347
}
4448
},
@@ -86,7 +90,10 @@ const testUtilsAgainst = (
8690
errors: [
8791
{
8892
messageId: 'details' as const,
89-
data: { callType },
93+
data: {
94+
callType,
95+
numOfArgs: 2,
96+
},
9097
column: 1,
9198
line: 1,
9299
},

src/rules/consistent-test-it.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
createRule,
99
getNodeName,
1010
isDescribeCall,
11-
isEachCall,
1211
isTestCaseCall,
1312
} from './utils';
1413

@@ -86,6 +85,8 @@ export default createRule<
8685
const funcNode =
8786
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
8887
? node.callee.tag
88+
: node.callee.type === AST_NODE_TYPES.CallExpression
89+
? node.callee.callee
8990
: node.callee;
9091

9192
if (
@@ -121,7 +122,7 @@ export default createRule<
121122
}
122123
},
123124
'CallExpression:exit'(node) {
124-
if (isDescribeCall(node) && !isEachCall(node)) {
125+
if (isDescribeCall(node)) {
125126
describeNestingLevel--;
126127
}
127128
},

0 commit comments

Comments
 (0)