Skip to content

Commit 014bd70

Browse files
authoredOct 19, 2023
feat(consistent-test-it): rewrite import statement as well (#274)
1 parent 5d45196 commit 014bd70

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
 

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

+26
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,36 @@ export default createEslintRule<
8484
const config = context.options[0] ?? {}
8585
const testFnKeyWork = config.fn || TestCaseName.test
8686
const testKeywordWithinDescribe = config?.withinDescribe || config?.fn || TestCaseName?.it
87+
const testFnDisabled = testFnKeyWork === testKeywordWithinDescribe ? testFnKeyWork : undefined
8788

8889
let describeNestingLevel = 0
8990

9091
return {
92+
ImportDeclaration(node: TSESTree.ImportDeclaration) {
93+
if (testFnDisabled == null)
94+
return
95+
if (node.source.type !== 'Literal' || node.source.value !== 'vitest')
96+
return
97+
98+
const oppositeTestKeyword = getOppositeTestKeyword(testFnDisabled)
99+
for (const specifier of node.specifiers) {
100+
if (specifier.type !== 'ImportSpecifier')
101+
continue
102+
if (specifier.local.name !== specifier.imported.name)
103+
continue
104+
if (specifier.local.name === oppositeTestKeyword) {
105+
context.report({
106+
node: specifier,
107+
data: { testFnKeyWork, oppositeTestKeyword },
108+
messageId: 'consistentMethod',
109+
fix: (fixer) => fixer.replaceText(
110+
specifier.local,
111+
testFnDisabled
112+
)
113+
})
114+
}
115+
}
116+
},
91117
CallExpression(node: TSESTree.CallExpression) {
92118
const vitestFnCall = parseVitestFnCall(node, context)
93119

‎tests/consistent-test-it.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ ruleTester.run(RULE_NAME, rule, {
7272
}
7373
}
7474
]
75+
},
76+
{
77+
code: 'import { test } from "vitest"\ntest("shows error", () => {});',
78+
options: [{ fn: TestCaseName.it }],
79+
output: 'import { it } from "vitest"\nit("shows error", () => {});',
80+
errors: [{ messageId: 'consistentMethod' }, { messageId: 'consistentMethod' }]
7581
}
7682
]
7783
})
@@ -268,6 +274,29 @@ ruleTester.run(RULE_NAME, rule, {
268274
}
269275
]
270276
},
277+
{
278+
code: 'import { it } from "vitest"\nit("foo")',
279+
output: 'import { test } from "vitest"\ntest("foo")',
280+
options: [
281+
{ withinDescribe: TestCaseName.test }
282+
],
283+
errors: [
284+
{
285+
messageId: 'consistentMethod',
286+
data: {
287+
testFnKeyWork: TestCaseName.test,
288+
oppositeTestKeyword: TestCaseName.it
289+
}
290+
},
291+
{
292+
messageId: 'consistentMethod',
293+
data: {
294+
testFnKeyWork: TestCaseName.test,
295+
oppositeTestKeyword: TestCaseName.it
296+
}
297+
}
298+
]
299+
},
271300
{
272301
code: 'describe("suite", () => { it("foo") })',
273302
output: 'describe("suite", () => { test("foo") })',

0 commit comments

Comments
 (0)
Please sign in to comment.