Skip to content

Commit bdf8e66

Browse files
authored
fix: Regression for valid-title and prefer-hooks-on-top when using test.scoped (#836)
* fix: Exclude scoped test calls from valid-title checks * fix: Exclude scoped and extend test calls from prefer-hooks-on-top checks * test: add tests
1 parent cc85c0a commit bdf8e66

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

src/rules/prefer-hooks-on-top.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { createEslintRule } from '../utils'
2-
import { isTypeOfVitestFnCall } from '../utils/parse-vitest-fn-call'
1+
import { createEslintRule, getAccessorValue } from '../utils'
2+
import {
3+
isTypeOfVitestFnCall,
4+
parseVitestFnCall,
5+
} from '../utils/parse-vitest-fn-call'
36

47
export const RULE_NAME = 'prefer-hooks-on-top'
58
type MessageIds = 'noHookOnTop'
@@ -23,7 +26,17 @@ export default createEslintRule<Options, MessageIds>({
2326
const hooksContext = [false]
2427
return {
2528
CallExpression(node) {
26-
if (isTypeOfVitestFnCall(node, context, ['test']))
29+
const vitestFnCall = parseVitestFnCall(node, context)
30+
31+
const hasExemptModifier = vitestFnCall?.members?.some((member) =>
32+
['extend', 'scoped'].includes(getAccessorValue(member)),
33+
)
34+
35+
if (
36+
vitestFnCall?.type &&
37+
['test', 'it'].includes(vitestFnCall.type) &&
38+
!hasExemptModifier
39+
)
2740
hooksContext[hooksContext.length - 1] = true
2841

2942
if (

src/rules/valid-title.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '@typescript-eslint/utils'
77
import {
88
createEslintRule,
9+
getAccessorValue,
910
getStringValue,
1011
isStringNode,
1112
StringNode,
@@ -222,12 +223,10 @@ export default createEslintRule<Options, MESSAGE_IDS>({
222223
return
223224

224225
// check if extend keyword have been used
225-
if (
226-
vitestFnCall.members &&
227-
vitestFnCall.members[0] &&
228-
vitestFnCall.members[0].type === AST_NODE_TYPES.Identifier &&
229-
vitestFnCall.members[0].name === 'extend'
230-
) {
226+
const hasExemptModifier = vitestFnCall.members.some((member) =>
227+
['extend', 'scoped'].includes(getAccessorValue(member)),
228+
)
229+
if (hasExemptModifier) {
231230
return
232231
}
233232

tests/prefer-hooks-on-top.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,44 @@ ruleTester.run(RULE_NAME, rule, {
2525
});
2626
});
2727
`,
28+
`
29+
describe('foo', () => {
30+
test.scoped({});
31+
beforeEach(() => {});
32+
afterEach(() => {});
33+
34+
test('bar', () => {
35+
someFn();
36+
});
37+
});
38+
`,
39+
`
40+
describe('foo', () => {
41+
it.scoped({});
42+
beforeEach(() => {});
43+
afterEach(() => {});
44+
45+
test('bar', () => {
46+
someFn();
47+
});
48+
});
49+
`,
50+
`
51+
import { test as baseTest } from 'vitest'
52+
53+
const test = baseTest.extend({})
54+
55+
beforeEach(() => {});
56+
afterEach(() => {});
57+
`,
58+
`
59+
import { it as baseIt } from 'vitest'
60+
61+
const it = baseIt.extend({})
62+
63+
beforeEach(() => {});
64+
afterEach(() => {});
65+
`,
2866
],
2967
invalid: [
3068
{

tests/valid-title.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ ruleTester.run(RULE_NAME, rule, {
563563
'test.concurrent(`foo`, function () {})',
564564
'test(`${foo}`, function () {})',
565565
'test.concurrent(`${foo}`, function () {})',
566+
'test.scoped({})',
567+
'it.scoped({})',
566568
"it('foo', function () {})",
567569
'it.each([])()',
568570
"it.concurrent('foo', function () {})",

0 commit comments

Comments
 (0)