Skip to content

Commit

Permalink
fix(typecheck): fix typecheck collect on Vite 6 (#6972)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Nov 27, 2024
1 parent 38e50f1 commit 7b35d13
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
17 changes: 12 additions & 5 deletions packages/vitest/src/typecheck/collect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { File, Suite, Test } from '@vitest/runner'
import type { Node } from 'estree'
import type { RawSourceMap } from 'vite-node'
import type { TestProject } from '../node/project'
import {
Expand Down Expand Up @@ -51,8 +52,6 @@ export async function collectTests(
if (!request) {
return null
}
// unwrap __vite_ssr_identity__ for Vite 6
request.code = request.code.replace(/__vite_ssr_identity__\((\w+\.\w+)\)/g, '( $1)')
const ast = await parseAstAsync(request.code)
const testFilepath = relative(ctx.config.root, filepath)
const projectName = ctx.name
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion test/cli/test/inspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('--inspect-brk stops at test file', async () => {

if (viteVersion[0] >= '6') {
// vite ssr transform wraps import by
// __vite_ssr_identity__(__vite_ssr_import_0__.test)(...)
// (0, __vite_ssr_import_0__.test)(...)
expect(result.scriptSource).toContain('test)("sum", () => {')
expect(result.scriptSource).toContain('expect)(1 + 1).toBe(2)')
}
Expand Down
7 changes: 1 addition & 6 deletions test/coverage-test/test/vue.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readdirSync } from 'node:fs'
import { resolve } from 'node:path'
import { version as viteVersion } from 'vite'
import { beforeAll, expect } from 'vitest'
import { isBrowser, isV8Provider, readCoverageMap, runVitest, test } from '../utils'

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

test('coverage results matches snapshot', async (ctx) => {
if (viteVersion[0] >= '6') {
ctx.skip()
}

test('coverage results matches snapshot', async () => {
const coverageMap = await readCoverageMap()
const summary = coverageMap.getCoverageSummary()

Expand Down

0 comments on commit 7b35d13

Please sign in to comment.