diff --git a/packages/vitest/src/typecheck/collect.ts b/packages/vitest/src/typecheck/collect.ts index 6f98c56c3e21..787d6de7c453 100644 --- a/packages/vitest/src/typecheck/collect.ts +++ b/packages/vitest/src/typecheck/collect.ts @@ -10,6 +10,7 @@ import { import { ancestor as walkAst } from 'acorn-walk' import { relative } from 'pathe' import { parseAstAsync } from 'vite' +import type { Node } from "estree" interface ParsedFile extends File { start: number @@ -51,8 +52,6 @@ export async function collectTests( if (!request) { return null } - // unwrap (0,...) for Vite 6 - request.code = request.code.replace(/\(0,(__vite_ssr_import_\d+__\.\w+)\)/g, '( $1)') const ast = await parseAstAsync(request.code) const testFilepath = relative(ctx.config.root, filepath) const projectName = ctx.name @@ -72,7 +71,7 @@ export async function collectTests( } file.file = file const definitions: LocalCallDefinition[] = [] - const getName = (callee: any): string | null => { + const getName = (callee: Node): string | null => { if (!callee) { return null } @@ -86,12 +85,20 @@ export async function collectTests( return getName(callee.tag) } if (callee.type === 'MemberExpression') { + const object = callee.object as any; // direct call as `__vite_ssr_exports_0__.test()` - if (callee.object?.name?.startsWith('__vite_ssr_')) { + if (object?.name?.startsWith('__vite_ssr_')) { return getName(callee.property) } // call as `__vite_ssr__.test.skip()` - return getName(callee.object?.property) + return getName(object?.property) + } + // unwrap (0, ...) + if (callee.type === "SequenceExpression" && callee.expressions.length === 2) { + const [e0, e1] = callee.expressions; + if (e0.type === "Literal" && e0.value === 0) { + return getName(e1); + } } return null }