From 035bd30af43f1215e65bf1b26c2ef2e6d174d3c8 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 17 Mar 2021 09:06:03 +1300 Subject: [PATCH] fix(consistent-test-it): properly handle `describe.each` (#796) Fixes #795 --- .../__tests__/consistent-test-it.test.ts | 41 +++++++++++++++++++ src/rules/consistent-test-it.ts | 3 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/rules/__tests__/consistent-test-it.test.ts b/src/rules/__tests__/consistent-test-it.test.ts index fc228aef6..9fc950da3 100644 --- a/src/rules/__tests__/consistent-test-it.test.ts +++ b/src/rules/__tests__/consistent-test-it.test.ts @@ -1,4 +1,5 @@ import { TSESLint } from '@typescript-eslint/experimental-utils'; +import dedent from 'dedent'; import resolveFrom from 'resolve-from'; import rule from '../consistent-test-it'; import { TestCaseName } from '../utils'; @@ -172,6 +173,46 @@ ruleTester.run('consistent-test-it with fn=test', rule, { }, ], }, + { + code: 'describe.each``("foo", () => { test.each``("bar") })', + output: 'describe.each``("foo", () => { it.each``("bar") })', + options: [{ fn: TestCaseName.it }], + errors: [ + { + messageId: 'consistentMethodWithinDescribe', + data: { + testKeywordWithinDescribe: TestCaseName.it, + oppositeTestKeyword: TestCaseName.test, + }, + }, + ], + }, + { + code: dedent` + describe.each()("%s", () => { + test("is valid, but should not be", () => {}); + + it("is not valid, but should be", () => {}); + }); + `, + output: dedent` + describe.each()("%s", () => { + it("is valid, but should not be", () => {}); + + it("is not valid, but should be", () => {}); + }); + `, + options: [{ fn: TestCaseName.test, withinDescribe: TestCaseName.it }], + errors: [ + { + messageId: 'consistentMethodWithinDescribe', + data: { + testKeywordWithinDescribe: TestCaseName.it, + oppositeTestKeyword: TestCaseName.test, + }, + }, + ], + }, { code: 'describe("suite", () => { it("foo") })', output: 'describe("suite", () => { test("foo") })', diff --git a/src/rules/consistent-test-it.ts b/src/rules/consistent-test-it.ts index 75cc9025e..dc6c8c12b 100644 --- a/src/rules/consistent-test-it.ts +++ b/src/rules/consistent-test-it.ts @@ -8,6 +8,7 @@ import { createRule, getNodeName, isDescribe, + isEachCall, isTestCase, } from './utils'; @@ -120,7 +121,7 @@ export default createRule< } }, 'CallExpression:exit'(node) { - if (isDescribe(node)) { + if (isDescribe(node) && !isEachCall(node)) { describeNestingLevel--; } },