Skip to content

Commit 7b35d13

Browse files
authoredNov 27, 2024··
fix(typecheck): fix typecheck collect on Vite 6 (#6972)
1 parent 38e50f1 commit 7b35d13

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed
 

‎packages/vitest/src/typecheck/collect.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { File, Suite, Test } from '@vitest/runner'
2+
import type { Node } from 'estree'
23
import type { RawSourceMap } from 'vite-node'
34
import type { TestProject } from '../node/project'
45
import {
@@ -51,8 +52,6 @@ export async function collectTests(
5152
if (!request) {
5253
return null
5354
}
54-
// unwrap __vite_ssr_identity__ for Vite 6
55-
request.code = request.code.replace(/__vite_ssr_identity__\((\w+\.\w+)\)/g, '( $1)')
5655
const ast = await parseAstAsync(request.code)
5756
const testFilepath = relative(ctx.config.root, filepath)
5857
const projectName = ctx.name
@@ -72,7 +71,7 @@ export async function collectTests(
7271
}
7372
file.file = file
7473
const definitions: LocalCallDefinition[] = []
75-
const getName = (callee: any): string | null => {
74+
const getName = (callee: Node): string | null => {
7675
if (!callee) {
7776
return null
7877
}
@@ -86,12 +85,20 @@ export async function collectTests(
8685
return getName(callee.tag)
8786
}
8887
if (callee.type === 'MemberExpression') {
88+
const object = callee.object as any
8989
// direct call as `__vite_ssr_exports_0__.test()`
90-
if (callee.object?.name?.startsWith('__vite_ssr_')) {
90+
if (object?.name?.startsWith('__vite_ssr_')) {
9191
return getName(callee.property)
9292
}
9393
// call as `__vite_ssr__.test.skip()`
94-
return getName(callee.object?.property)
94+
return getName(object?.property)
95+
}
96+
// unwrap (0, ...)
97+
if (callee.type === 'SequenceExpression' && callee.expressions.length === 2) {
98+
const [e0, e1] = callee.expressions
99+
if (e0.type === 'Literal' && e0.value === 0) {
100+
return getName(e1)
101+
}
95102
}
96103
return null
97104
}

‎test/cli/test/inspect.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('--inspect-brk stops at test file', async () => {
3838

3939
if (viteVersion[0] >= '6') {
4040
// vite ssr transform wraps import by
41-
// __vite_ssr_identity__(__vite_ssr_import_0__.test)(...)
41+
// (0, __vite_ssr_import_0__.test)(...)
4242
expect(result.scriptSource).toContain('test)("sum", () => {')
4343
expect(result.scriptSource).toContain('expect)(1 + 1).toBe(2)')
4444
}

‎test/coverage-test/test/vue.test.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { readdirSync } from 'node:fs'
22
import { resolve } from 'node:path'
3-
import { version as viteVersion } from 'vite'
43
import { beforeAll, expect } from 'vitest'
54
import { isBrowser, isV8Provider, readCoverageMap, runVitest, test } from '../utils'
65

@@ -21,11 +20,7 @@ test('files should not contain query parameters', () => {
2120
expect(files).not.toContain('Counter.component.ts?vue&type=script&src=true&lang.ts.html')
2221
})
2322

24-
test('coverage results matches snapshot', async (ctx) => {
25-
if (viteVersion[0] >= '6') {
26-
ctx.skip()
27-
}
28-
23+
test('coverage results matches snapshot', async () => {
2924
const coverageMap = await readCoverageMap()
3025
const summary = coverageMap.getCoverageSummary()
3126

0 commit comments

Comments
 (0)
Please sign in to comment.