Skip to content

Commit

Permalink
feat(consistent-test-it): rewrite import statement as well (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Oct 19, 2023
1 parent 5d45196 commit 014bd70
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/rules/consistent-test-it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions tests/consistent-test-it.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }]
}
]
})
Expand Down Expand Up @@ -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") })',
Expand Down

0 comments on commit 014bd70

Please sign in to comment.