diff --git a/src/rules/consistent-test-it.ts b/src/rules/consistent-test-it.ts index 4bacef2..802d5a0 100644 --- a/src/rules/consistent-test-it.ts +++ b/src/rules/consistent-test-it.ts @@ -84,10 +84,36 @@ export default createEslintRule< const config = context.options[0] ?? {} const testFnKeyWork = config.fn || TestCaseName.test const testKeywordWithinDescribe = config?.withinDescribe || config?.fn || TestCaseName?.it + const testFnDisabled = testFnKeyWork === testKeywordWithinDescribe ? testFnKeyWork : undefined let describeNestingLevel = 0 return { + ImportDeclaration(node: TSESTree.ImportDeclaration) { + if (testFnDisabled == null) + return + if (node.source.type !== 'Literal' || node.source.value !== 'vitest') + return + + const oppositeTestKeyword = getOppositeTestKeyword(testFnDisabled) + for (const specifier of node.specifiers) { + if (specifier.type !== 'ImportSpecifier') + continue + if (specifier.local.name !== specifier.imported.name) + continue + if (specifier.local.name === oppositeTestKeyword) { + context.report({ + node: specifier, + data: { testFnKeyWork, oppositeTestKeyword }, + messageId: 'consistentMethod', + fix: (fixer) => fixer.replaceText( + specifier.local, + testFnDisabled + ) + }) + } + } + }, CallExpression(node: TSESTree.CallExpression) { const vitestFnCall = parseVitestFnCall(node, context) diff --git a/tests/consistent-test-it.test.ts b/tests/consistent-test-it.test.ts index edf309d..e6dbe64 100644 --- a/tests/consistent-test-it.test.ts +++ b/tests/consistent-test-it.test.ts @@ -72,6 +72,12 @@ ruleTester.run(RULE_NAME, rule, { } } ] + }, + { + code: 'import { test } from "vitest"\ntest("shows error", () => {});', + options: [{ fn: TestCaseName.it }], + output: 'import { it } from "vitest"\nit("shows error", () => {});', + errors: [{ messageId: 'consistentMethod' }, { messageId: 'consistentMethod' }] } ] }) @@ -268,6 +274,29 @@ ruleTester.run(RULE_NAME, rule, { } ] }, + { + code: 'import { it } from "vitest"\nit("foo")', + output: 'import { test } from "vitest"\ntest("foo")', + options: [ + { withinDescribe: TestCaseName.test } + ], + errors: [ + { + messageId: 'consistentMethod', + data: { + testFnKeyWork: TestCaseName.test, + oppositeTestKeyword: TestCaseName.it + } + }, + { + messageId: 'consistentMethod', + data: { + testFnKeyWork: TestCaseName.test, + oppositeTestKeyword: TestCaseName.it + } + } + ] + }, { code: 'describe("suite", () => { it("foo") })', output: 'describe("suite", () => { test("foo") })',