Skip to content

Commit e1dc42d

Browse files
k-yleG-Rath
andauthored
fix: proper support for it.each (#722)
* fix: proper it.each support * fix: apply code review suggestions Co-authored-by: Gareth Jones <[email protected]> Co-authored-by: Gareth Jones <[email protected]>
1 parent 7458356 commit e1dc42d

9 files changed

+203
-10
lines changed

src/rules/__tests__/consistent-test-it.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ ruleTester.run('consistent-test-it with fn=test', rule, {
144144
},
145145
],
146146
},
147+
{
148+
code: 'it.each``("foo")',
149+
output: 'test.each``("foo")',
150+
options: [{ fn: TestCaseName.test }],
151+
errors: [
152+
{
153+
messageId: 'consistentMethod',
154+
data: {
155+
testKeyword: TestCaseName.test,
156+
oppositeTestKeyword: TestCaseName.it,
157+
},
158+
},
159+
],
160+
},
147161
{
148162
code: 'describe("suite", () => { it("foo") })',
149163
output: 'describe("suite", () => { test("foo") })',
@@ -285,6 +299,20 @@ ruleTester.run('consistent-test-it with fn=it', rule, {
285299
},
286300
],
287301
},
302+
{
303+
code: 'test.each``("foo")',
304+
output: 'it.each``("foo")',
305+
options: [{ fn: TestCaseName.it }],
306+
errors: [
307+
{
308+
messageId: 'consistentMethod',
309+
data: {
310+
testKeyword: TestCaseName.it,
311+
oppositeTestKeyword: TestCaseName.test,
312+
},
313+
},
314+
],
315+
},
288316
{
289317
code: 'describe("suite", () => { test("foo") })',
290318
output: 'describe("suite", () => { it("foo") })',

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

+32
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ ruleTester.run('no-disabled-tests', rule, {
8989
code: 'test.skip("foo", function () {})',
9090
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
9191
},
92+
{
93+
code: 'it.skip.each``("foo", function () {})',
94+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
95+
},
96+
{
97+
code: 'test.skip.each``("foo", function () {})',
98+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
99+
},
100+
{
101+
code: 'it.skip.each([])("foo", function () {})',
102+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
103+
},
104+
{
105+
code: 'test.skip.each([])("foo", function () {})',
106+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
107+
},
92108
{
93109
code: 'test.concurrent.skip("foo", function () {})',
94110
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
@@ -109,6 +125,22 @@ ruleTester.run('no-disabled-tests', rule, {
109125
code: 'xtest("foo", function () {})',
110126
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
111127
},
128+
{
129+
code: 'xit.each``("foo", function () {})',
130+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
131+
},
132+
{
133+
code: 'xtest.each``("foo", function () {})',
134+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
135+
},
136+
{
137+
code: 'xit.each([])("foo", function () {})',
138+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
139+
},
140+
{
141+
code: 'xtest.each([])("foo", function () {})',
142+
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
143+
},
112144
{
113145
code: 'it("has title but no callback")',
114146
errors: [{ messageId: 'missingFunction', column: 1, line: 1 }],

src/rules/__tests__/no-done-callback.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ ruleTester.run('no-done-callback', rule, {
2020
'it.each()("something", ({ a, b }) => {})',
2121
'it.each([])("something", (a, b) => {})',
2222
'it.each``("something", ({ a, b }) => {})',
23+
'it.each([])("something", (a, b) => { a(); b(); })',
24+
'it.each``("something", ({ a, b }) => { a(); b(); })',
2325
'test("something", async function () {})',
2426
'test("something", someArg)',
2527
'beforeEach(() => {})',
@@ -390,5 +392,25 @@ ruleTester.run('no-done-callback', rule, {
390392
},
391393
],
392394
},
395+
{
396+
code: 'test.each``("something", ({ a, b }, done) => { done(); })',
397+
errors: [
398+
{
399+
messageId: 'noDoneCallback',
400+
line: 1,
401+
column: 37,
402+
},
403+
],
404+
},
405+
{
406+
code: 'it.each``("something", ({ a, b }, done) => { done(); })',
407+
errors: [
408+
{
409+
messageId: 'noDoneCallback',
410+
line: 1,
411+
column: 35,
412+
},
413+
],
414+
},
393415
],
394416
});

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

+50
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,55 @@ ruleTester.run('no-test-prefixes', rule, {
106106
},
107107
],
108108
},
109+
{
110+
code: 'xit.each``("foo", function () {})',
111+
output: 'it.skip.each``("foo", function () {})',
112+
parserOptions: { ecmaVersion: 6 },
113+
errors: [
114+
{
115+
messageId: 'usePreferredName',
116+
data: { preferredNodeName: 'it.skip.each' },
117+
column: 1,
118+
line: 1,
119+
},
120+
],
121+
},
122+
{
123+
code: 'xtest.each``("foo", function () {})',
124+
output: 'test.skip.each``("foo", function () {})',
125+
parserOptions: { ecmaVersion: 6 },
126+
errors: [
127+
{
128+
messageId: 'usePreferredName',
129+
data: { preferredNodeName: 'test.skip.each' },
130+
column: 1,
131+
line: 1,
132+
},
133+
],
134+
},
135+
{
136+
code: 'xit.each([])("foo", function () {})',
137+
output: 'it.skip.each([])("foo", function () {})',
138+
errors: [
139+
{
140+
messageId: 'usePreferredName',
141+
data: { preferredNodeName: 'it.skip.each' },
142+
column: 1,
143+
line: 1,
144+
},
145+
],
146+
},
147+
{
148+
code: 'xtest.each([])("foo", function () {})',
149+
output: 'test.skip.each([])("foo", function () {})',
150+
errors: [
151+
{
152+
messageId: 'usePreferredName',
153+
data: { preferredNodeName: 'test.skip.each' },
154+
column: 1,
155+
line: 1,
156+
},
157+
],
158+
},
109159
],
110160
});

src/rules/consistent-test-it.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ export default createRule<
8282
describeNestingLevel++;
8383
}
8484

85+
const funcNode =
86+
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
87+
? node.callee.tag
88+
: node.callee;
89+
8590
if (
8691
isTestCase(node) &&
8792
describeNestingLevel === 0 &&
@@ -93,7 +98,7 @@ export default createRule<
9398
messageId: 'consistentMethod',
9499
node: node.callee,
95100
data: { testKeyword, oppositeTestKeyword },
96-
fix: buildFixer(node.callee, nodeName, testKeyword),
101+
fix: buildFixer(funcNode, nodeName, testKeyword),
97102
});
98103
}
99104

@@ -110,7 +115,7 @@ export default createRule<
110115
messageId: 'consistentMethodWithinDescribe',
111116
node: node.callee,
112117
data: { testKeywordWithinDescribe, oppositeTestKeyword },
113-
fix: buildFixer(node.callee, nodeName, testKeywordWithinDescribe),
118+
fix: buildFixer(funcNode, nodeName, testKeywordWithinDescribe),
114119
});
115120
}
116121
},

src/rules/no-disabled-tests.ts

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export default createRule({
3939
CallExpression(node) {
4040
const functionName = getNodeName(node.callee);
4141

42+
// prevent duplicate warnings for it.each()()
43+
if (node.callee.type === 'CallExpression') {
44+
return;
45+
}
46+
4247
switch (functionName) {
4348
case 'describe.skip':
4449
context.report({ messageId: 'skippedTestSuite', node });
@@ -48,6 +53,10 @@ export default createRule({
4853
case 'it.concurrent.skip':
4954
case 'test.skip':
5055
case 'test.concurrent.skip':
56+
case 'it.skip.each':
57+
case 'test.skip.each':
58+
case 'xit.each':
59+
case 'xtest.each':
5160
context.report({ messageId: 'skippedTest', node });
5261
break;
5362
}

src/rules/no-done-callback.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ import {
22
AST_NODE_TYPES,
33
TSESTree,
44
} from '@typescript-eslint/experimental-utils';
5-
import { createRule, isFunction, isHook, isTestCase } from './utils';
5+
import {
6+
createRule,
7+
getNodeName,
8+
isFunction,
9+
isHook,
10+
isTestCase,
11+
} from './utils';
612

713
const findCallbackArg = (
814
node: TSESTree.CallExpression,
15+
isJestEach: boolean,
916
): TSESTree.CallExpression['arguments'][0] | null => {
17+
if (isJestEach) {
18+
return node.arguments[1];
19+
}
20+
1021
if (isHook(node) && node.arguments.length >= 1) {
1122
return node.arguments[0];
1223
}
@@ -41,17 +52,31 @@ export default createRule({
4152
create(context) {
4253
return {
4354
CallExpression(node) {
44-
const callback = findCallbackArg(node);
55+
// done is the second argument for it.each, not the first
56+
const isJestEach = getNodeName(node.callee)?.endsWith('.each') ?? false;
57+
58+
if (
59+
isJestEach &&
60+
node.callee.type !== AST_NODE_TYPES.TaggedTemplateExpression
61+
) {
62+
// isJestEach but not a TaggedTemplateExpression, so this must be
63+
// the `jest.each([])()` syntax which this rule doesn't support due
64+
// to its complexity (see jest-community/eslint-plugin-jest#710)
65+
return;
66+
}
67+
68+
const callback = findCallbackArg(node, isJestEach);
69+
const callbackArgIndex = Number(isJestEach);
4570

4671
if (
4772
!callback ||
4873
!isFunction(callback) ||
49-
callback.params.length !== 1
74+
callback.params.length !== 1 + callbackArgIndex
5075
) {
5176
return;
5277
}
5378

54-
const [argument] = callback.params;
79+
const argument = callback.params[callbackArgIndex];
5580

5681
if (argument.type !== AST_NODE_TYPES.Identifier) {
5782
context.report({

src/rules/no-test-prefixes.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
12
import { createRule, getNodeName, isDescribe, isTestCase } from './utils';
23

34
export default createRule({
@@ -27,12 +28,17 @@ export default createRule({
2728

2829
if (!preferredNodeName) return;
2930

31+
const funcNode =
32+
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
33+
? node.callee.tag
34+
: node.callee;
35+
3036
context.report({
3137
messageId: 'usePreferredName',
3238
node: node.callee,
3339
data: { preferredNodeName },
3440
fix(fixer) {
35-
return [fixer.replaceText(node.callee, preferredNodeName)];
41+
return [fixer.replaceText(funcNode, preferredNodeName)];
3642
},
3743
});
3844
},
@@ -43,12 +49,14 @@ export default createRule({
4349
function getPreferredNodeName(nodeName: string) {
4450
const firstChar = nodeName.charAt(0);
4551

52+
const suffix = nodeName.endsWith('.each') ? '.each' : '';
53+
4654
if (firstChar === 'f') {
47-
return `${nodeName.slice(1)}.only`;
55+
return `${nodeName.slice(1).replace('.each', '')}.only${suffix}`;
4856
}
4957

5058
if (firstChar === 'x') {
51-
return `${nodeName.slice(1)}.skip`;
59+
return `${nodeName.slice(1).replace('.each', '')}.skip${suffix}`;
5260
}
5361

5462
return null;

src/rules/utils.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,16 @@ export interface JestFunctionCallExpressionWithIdentifierCallee<
580580
callee: JestFunctionIdentifier<FunctionName>;
581581
}
582582

583+
interface JestFunctionCallExpressionWithTaggedTemplateCallee
584+
extends TSESTree.CallExpression {
585+
callee: TSESTree.TaggedTemplateExpression;
586+
}
587+
583588
export type JestFunctionCallExpression<
584589
FunctionName extends JestFunctionName = JestFunctionName
585590
> =
586591
| JestFunctionCallExpressionWithMemberExpressionCallee<FunctionName>
592+
| JestFunctionCallExpressionWithTaggedTemplateCallee
587593
| JestFunctionCallExpressionWithIdentifierCallee<FunctionName>;
588594

589595
const joinNames = (a: string | null, b: string | null): string | null =>
@@ -592,7 +598,8 @@ const joinNames = (a: string | null, b: string | null): string | null =>
592598
export function getNodeName(
593599
node:
594600
| JestFunctionMemberExpression<JestFunctionName>
595-
| JestFunctionIdentifier<JestFunctionName>,
601+
| JestFunctionIdentifier<JestFunctionName>
602+
| TSESTree.TaggedTemplateExpression,
596603
): string;
597604
export function getNodeName(node: TSESTree.Node): string | null;
598605
export function getNodeName(node: TSESTree.Node): string | null {
@@ -601,6 +608,8 @@ export function getNodeName(node: TSESTree.Node): string | null {
601608
}
602609

603610
switch (node.type) {
611+
case AST_NODE_TYPES.TaggedTemplateExpression:
612+
return getNodeName(node.tag);
604613
case AST_NODE_TYPES.MemberExpression:
605614
return joinNames(getNodeName(node.object), getNodeName(node.property));
606615
case AST_NODE_TYPES.NewExpression:
@@ -651,6 +660,11 @@ export const isTestCase = (
651660
): node is JestFunctionCallExpression<TestCaseName> =>
652661
(node.callee.type === AST_NODE_TYPES.Identifier &&
653662
TestCaseName.hasOwnProperty(node.callee.name)) ||
663+
// e.g. it.each``()
664+
(node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
665+
node.callee.tag.type === AST_NODE_TYPES.MemberExpression &&
666+
isSupportedAccessor(node.callee.tag.property, TestCaseProperty.each)) ||
667+
// e.g. it.concurrent.{skip,only}
654668
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
655669
node.callee.property.type === AST_NODE_TYPES.Identifier &&
656670
TestCaseProperty.hasOwnProperty(node.callee.property.name) &&

0 commit comments

Comments
 (0)