Skip to content

Commit 7c8d75a

Browse files
authored
fix: revert change causing regressions for test.each (#713)
Fixes #710 Fixes #711
1 parent fe80f2f commit 7c8d75a

9 files changed

+11
-209
lines changed

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

-28
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,6 @@ 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-
},
161147
{
162148
code: 'describe("suite", () => { it("foo") })',
163149
output: 'describe("suite", () => { test("foo") })',
@@ -299,20 +285,6 @@ ruleTester.run('consistent-test-it with fn=it', rule, {
299285
},
300286
],
301287
},
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-
},
316288
{
317289
code: 'describe("suite", () => { test("foo") })',
318290
output: 'describe("suite", () => { it("foo") })',

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

-32
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,6 @@ ruleTester.run('no-disabled-tests', rule, {
8888
code: 'test.skip("foo", function () {})',
8989
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
9090
},
91-
{
92-
code: 'it.skip.each``("foo", function () {})',
93-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
94-
},
95-
{
96-
code: 'test.skip.each``("foo", function () {})',
97-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
98-
},
99-
{
100-
code: 'it.skip.each([])("foo", function () {})',
101-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
102-
},
103-
{
104-
code: 'test.skip.each([])("foo", function () {})',
105-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
106-
},
10791
{
10892
code: 'test.concurrent.skip("foo", function () {})',
10993
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
@@ -124,22 +108,6 @@ ruleTester.run('no-disabled-tests', rule, {
124108
code: 'xtest("foo", function () {})',
125109
errors: [{ messageId: 'disabledTest', column: 1, line: 1 }],
126110
},
127-
{
128-
code: 'xit.each``("foo", function () {})',
129-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
130-
},
131-
{
132-
code: 'xtest.each``("foo", function () {})',
133-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
134-
},
135-
{
136-
code: 'xit.each([])("foo", function () {})',
137-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
138-
},
139-
{
140-
code: 'xtest.each([])("foo", function () {})',
141-
errors: [{ messageId: 'skippedTest', column: 1, line: 1 }],
142-
},
143111
{
144112
code: 'it("has title but no callback")',
145113
errors: [{ messageId: 'missingFunction', column: 1, line: 1 }],

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

+1-40
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ruleTester.run('no-done-callback', rule, {
1818
'test.each``("something", ({ a, b }) => {})',
1919
'test.each()("something", ({ a, b }) => {})',
2020
'it.each()("something", ({ a, b }) => {})',
21+
'it.each([])("something", (a, b) => {})',
2122
'it.each``("something", ({ a, b }) => {})',
2223
'test("something", async function () {})',
2324
'test("something", someArg)',
@@ -389,45 +390,5 @@ ruleTester.run('no-done-callback', rule, {
389390
},
390391
],
391392
},
392-
{
393-
code: 'test.each``("something", ({ a, b }, done) => { done(); })',
394-
errors: [
395-
{
396-
messageId: 'noDoneCallback',
397-
line: 1,
398-
column: 37,
399-
},
400-
],
401-
},
402-
{
403-
code: 'test.each()("something", ({ a, b }, done) => { done(); })',
404-
errors: [
405-
{
406-
messageId: 'noDoneCallback',
407-
line: 1,
408-
column: 37,
409-
},
410-
],
411-
},
412-
{
413-
code: 'it.each``("something", ({ a, b }, done) => { done(); })',
414-
errors: [
415-
{
416-
messageId: 'noDoneCallback',
417-
line: 1,
418-
column: 35,
419-
},
420-
],
421-
},
422-
{
423-
code: 'it.each()("something", ({ a, b }, done) => { done(); })',
424-
errors: [
425-
{
426-
messageId: 'noDoneCallback',
427-
line: 1,
428-
column: 35,
429-
},
430-
],
431-
},
432393
],
433394
});

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

-50
Original file line numberDiff line numberDiff line change
@@ -106,55 +106,5 @@ 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-
},
159109
],
160110
});

src/rules/consistent-test-it.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ 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-
9085
if (
9186
isTestCase(node) &&
9287
describeNestingLevel === 0 &&
@@ -98,7 +93,7 @@ export default createRule<
9893
messageId: 'consistentMethod',
9994
node: node.callee,
10095
data: { testKeyword, oppositeTestKeyword },
101-
fix: buildFixer(funcNode, nodeName, testKeyword),
96+
fix: buildFixer(node.callee, nodeName, testKeyword),
10297
});
10398
}
10499

@@ -115,7 +110,7 @@ export default createRule<
115110
messageId: 'consistentMethodWithinDescribe',
116111
node: node.callee,
117112
data: { testKeywordWithinDescribe, oppositeTestKeyword },
118-
fix: buildFixer(funcNode, nodeName, testKeywordWithinDescribe),
113+
fix: buildFixer(node.callee, nodeName, testKeywordWithinDescribe),
119114
});
120115
}
121116
},

src/rules/no-disabled-tests.ts

-7
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ 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') return;
44-
4542
switch (functionName) {
4643
case 'describe.skip':
4744
context.report({ messageId: 'skippedTestSuite', node });
@@ -51,10 +48,6 @@ export default createRule({
5148
case 'it.concurrent.skip':
5249
case 'test.skip':
5350
case 'test.concurrent.skip':
54-
case 'it.skip.each':
55-
case 'test.skip.each':
56-
case 'xit.each':
57-
case 'xtest.each':
5851
context.report({ messageId: 'skippedTest', node });
5952
break;
6053
}

src/rules/no-done-callback.ts

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

137
const findCallbackArg = (
148
node: TSESTree.CallExpression,
15-
isJestEach: boolean,
169
): TSESTree.CallExpression['arguments'][0] | null => {
17-
if (isJestEach) {
18-
return node.arguments[1];
19-
}
20-
2110
if (isHook(node) && node.arguments.length >= 1) {
2211
return node.arguments[0];
2312
}
@@ -52,21 +41,17 @@ export default createRule({
5241
create(context) {
5342
return {
5443
CallExpression(node) {
55-
// done is the second argument for it.each, not the first
56-
const isJestEach = getNodeName(node.callee)?.endsWith('.each') ?? false;
57-
58-
const callback = findCallbackArg(node, isJestEach);
59-
const callbackArgIndex = Number(isJestEach);
44+
const callback = findCallbackArg(node);
6045

6146
if (
6247
!callback ||
6348
!isFunction(callback) ||
64-
callback.params.length !== 1 + callbackArgIndex
49+
callback.params.length !== 1
6550
) {
6651
return;
6752
}
6853

69-
const argument = callback.params[callbackArgIndex];
54+
const [argument] = callback.params;
7055

7156
if (argument.type !== AST_NODE_TYPES.Identifier) {
7257
context.report({

src/rules/no-test-prefixes.ts

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

43
export default createRule({
@@ -28,17 +27,12 @@ export default createRule({
2827

2928
if (!preferredNodeName) return;
3029

31-
const funcNode =
32-
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
33-
? node.callee.tag
34-
: node.callee;
35-
3630
context.report({
3731
messageId: 'usePreferredName',
3832
node: node.callee,
3933
data: { preferredNodeName },
4034
fix(fixer) {
41-
return [fixer.replaceText(funcNode, preferredNodeName)];
35+
return [fixer.replaceText(node.callee, preferredNodeName)];
4236
},
4337
});
4438
},
@@ -49,14 +43,12 @@ export default createRule({
4943
function getPreferredNodeName(nodeName: string) {
5044
const firstChar = nodeName.charAt(0);
5145

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

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

6254
return null;

src/rules/utils.ts

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

583-
interface JestFunctionCallExpressionWithTaggedTemplateCallee
584-
extends TSESTree.CallExpression {
585-
callee: TSESTree.TaggedTemplateExpression;
586-
}
587-
588583
export type JestFunctionCallExpression<
589584
FunctionName extends JestFunctionName = JestFunctionName
590585
> =
591586
| JestFunctionCallExpressionWithMemberExpressionCallee<FunctionName>
592-
| JestFunctionCallExpressionWithTaggedTemplateCallee
593587
| JestFunctionCallExpressionWithIdentifierCallee<FunctionName>;
594588

595589
const joinNames = (a: string | null, b: string | null): string | null =>
@@ -598,8 +592,7 @@ const joinNames = (a: string | null, b: string | null): string | null =>
598592
export function getNodeName(
599593
node:
600594
| JestFunctionMemberExpression<JestFunctionName>
601-
| JestFunctionIdentifier<JestFunctionName>
602-
| TSESTree.TaggedTemplateExpression,
595+
| JestFunctionIdentifier<JestFunctionName>,
603596
): string;
604597
export function getNodeName(node: TSESTree.Node): string | null;
605598
export function getNodeName(node: TSESTree.Node): string | null {
@@ -608,8 +601,6 @@ export function getNodeName(node: TSESTree.Node): string | null {
608601
}
609602

610603
switch (node.type) {
611-
case AST_NODE_TYPES.TaggedTemplateExpression:
612-
return getNodeName(node.tag);
613604
case AST_NODE_TYPES.MemberExpression:
614605
return joinNames(getNodeName(node.object), getNodeName(node.property));
615606
case AST_NODE_TYPES.NewExpression:
@@ -660,11 +651,6 @@ export const isTestCase = (
660651
): node is JestFunctionCallExpression<TestCaseName> =>
661652
(node.callee.type === AST_NODE_TYPES.Identifier &&
662653
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}
668654
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
669655
node.callee.property.type === AST_NODE_TYPES.Identifier &&
670656
TestCaseProperty.hasOwnProperty(node.callee.property.name) &&

0 commit comments

Comments
 (0)