Skip to content

Commit

Permalink
fix(vitest/no-done-callback): do not report when inside concurrent de…
Browse files Browse the repository at this point in the history
…scribe (#321)
  • Loading branch information
Haberkamp committed Dec 11, 2023
1 parent be27d83 commit 65e8cef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
14 changes: 14 additions & 0 deletions docs/rules/no-done-callback.md
Expand Up @@ -56,4 +56,18 @@ test('foo', async () => {
resolve()
}, 1000))
})

test.concurrent('foo', ({ expect }) => {
expect(1).toMatchSnapshot();
});

test.concurrent('foo', (context) => {
context.expect(1).toBe(1);
});

describe.concurrent('foo', () => {
test('foo', ({ expect }) => {
expect(1).toBe(1);
});
});
```
20 changes: 16 additions & 4 deletions src/rules/no-done-callback.ts
@@ -1,6 +1,6 @@
import { TSESLint, AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'
import { createEslintRule, getNodeName, isFunction } from '../utils'
import { parseVitestFnCall } from '../utils/parseVitestFnCall'
import { createEslintRule, getNodeName, isFunction, isSupportedAccessor } from '../utils'
import { isTypeOfVitestFnCall, parseVitestFnCall } from '../utils/parseVitestFnCall'

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

const isVitestConcurrent = getNodeName(node.callee)?.endsWith('.concurrent') ?? false
if (isVitestConcurrent) return
const isInsideConcurrentTestOrDescribe = context.getAncestors().some((ancestor) => {
if (ancestor.type !== AST_NODE_TYPES.CallExpression) return false

const isNotInsideDescribeOrTest = !isTypeOfVitestFnCall(ancestor, context, ['describe', 'test'])
if (isNotInsideDescribeOrTest) return false

const isTestRunningConcurrently =
ancestor.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(ancestor.callee.property, 'concurrent')

return isTestRunningConcurrently
})

if (isInsideConcurrentTestOrDescribe) return;

const callback = findCallbackArg(node, isVitestEach, context)
const callbackArgIndex = Number(isVitestEach)
Expand Down
4 changes: 3 additions & 1 deletion tests/no-done-callback.test.ts
Expand Up @@ -21,7 +21,9 @@ ruleTester.run(RULE_NAME, rule, {
'beforeAll(async () => {})',
'afterAll(() => {})',
'afterAll(async function () {})',
'afterAll(async function () {}, 5)'
'afterAll(async function () {}, 5)',
'describe.concurrent("something", () => { it("something", ({ expect }) => { }) })',
'describe.concurrent("something", () => { it("something", context => { }) })'
],
invalid: [
{
Expand Down

0 comments on commit 65e8cef

Please sign in to comment.