Skip to content

Commit 65e8cef

Browse files
authoredDec 11, 2023
fix(vitest/no-done-callback): do not report when inside concurrent describe (#321)
1 parent be27d83 commit 65e8cef

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed
 

‎docs/rules/no-done-callback.md

+14
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,18 @@ test('foo', async () => {
5656
resolve()
5757
}, 1000))
5858
})
59+
60+
test.concurrent('foo', ({ expect }) => {
61+
expect(1).toMatchSnapshot();
62+
});
63+
64+
test.concurrent('foo', (context) => {
65+
context.expect(1).toBe(1);
66+
});
67+
68+
describe.concurrent('foo', () => {
69+
test('foo', ({ expect }) => {
70+
expect(1).toBe(1);
71+
});
72+
});
5973
```

‎src/rules/no-done-callback.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TSESLint, AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'
2-
import { createEslintRule, getNodeName, isFunction } from '../utils'
3-
import { parseVitestFnCall } from '../utils/parseVitestFnCall'
2+
import { createEslintRule, getNodeName, isFunction, isSupportedAccessor } from '../utils'
3+
import { isTypeOfVitestFnCall, parseVitestFnCall } from '../utils/parseVitestFnCall'
44

55
export const RULE_NAME = 'no-done-callback'
66
export type MessageIds = 'noDoneCallback' | 'suggestWrappingInPromise' | 'useAwaitInsteadOfCallback';
@@ -46,8 +46,20 @@ export default createEslintRule<Options, MessageIds>({
4646
if (isVitestEach && node.callee.type !== AST_NODE_TYPES.TaggedTemplateExpression)
4747
return
4848

49-
const isVitestConcurrent = getNodeName(node.callee)?.endsWith('.concurrent') ?? false
50-
if (isVitestConcurrent) return
49+
const isInsideConcurrentTestOrDescribe = context.getAncestors().some((ancestor) => {
50+
if (ancestor.type !== AST_NODE_TYPES.CallExpression) return false
51+
52+
const isNotInsideDescribeOrTest = !isTypeOfVitestFnCall(ancestor, context, ['describe', 'test'])
53+
if (isNotInsideDescribeOrTest) return false
54+
55+
const isTestRunningConcurrently =
56+
ancestor.callee.type === AST_NODE_TYPES.MemberExpression &&
57+
isSupportedAccessor(ancestor.callee.property, 'concurrent')
58+
59+
return isTestRunningConcurrently
60+
})
61+
62+
if (isInsideConcurrentTestOrDescribe) return;
5163

5264
const callback = findCallbackArg(node, isVitestEach, context)
5365
const callbackArgIndex = Number(isVitestEach)

‎tests/no-done-callback.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ ruleTester.run(RULE_NAME, rule, {
2121
'beforeAll(async () => {})',
2222
'afterAll(() => {})',
2323
'afterAll(async function () {})',
24-
'afterAll(async function () {}, 5)'
24+
'afterAll(async function () {}, 5)',
25+
'describe.concurrent("something", () => { it("something", ({ expect }) => { }) })',
26+
'describe.concurrent("something", () => { it("something", context => { }) })'
2527
],
2628
invalid: [
2729
{

0 commit comments

Comments
 (0)
Please sign in to comment.