Skip to content

Commit 7f91105

Browse files
committed
fix: ignore __vi_import__ and support Vitest.test
Closes #638
1 parent debabb8 commit 7f91105

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

src/worker/collect.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ export function astParseFile(filepath: string, code: string) {
9999
) {
100100
return callee.object?.name
101101
}
102-
// direct call as `__vite_ssr_exports_0__.test()`
103-
if (callee.object?.name?.startsWith('__vite_ssr_')) {
102+
if (
103+
// direct call as `__vite_ssr_exports_0__.test()`
104+
callee.object?.name?.startsWith('__vite_ssr_')
105+
// call as `__vite_ssr_exports_0__.Vitest.test`,
106+
// this is a special case for using Vitest namespaces popular in Effect
107+
|| (callee.object?.object?.name?.startsWith('__vite_ssr_') && callee.object?.property?.name === 'Vitest')
108+
) {
104109
return getName(callee.property)
105110
}
106111
// call as `__vite_ssr__.test.skip()`
@@ -167,7 +172,11 @@ export function astParseFile(filepath: string, code: string) {
167172
message = message.slice(2)
168173
}
169174

170-
message = message.replace(/__vite_ssr_import_\d+__\./g, '')
175+
message = message
176+
// Vite SSR injects these
177+
.replace(/__vite_ssr_import_\d+__\./g, '')
178+
// Vitest module mocker injects these
179+
.replace(/__vi_import_\d+__/g, '')
171180

172181
// cannot statically analyze, so we always skip it
173182
if (mode === 'skipIf' || mode === 'runIf') {

test/e2e/discovery.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { describe, expect, it, onTestFinished } from 'vitest'
44
import { createVitest } from 'vitest/node'
55
import { astCollectTests } from '../../src/worker/collect'
66

7+
const variableFixture = 'test-from-vitest-variable.ts'
8+
79
describe('can discover tests', () => {
810
it.for([
911
'todo-import-suite.ts',
1012
'todo-globals-suite.ts',
13+
variableFixture,
1114
])('can discover todo tests inside a suite in %s', async (fixture) => {
1215
const vitest = await createVitest('test', { config: false })
1316
onTestFinished(() => vitest.close())
@@ -34,7 +37,7 @@ describe('can discover tests', () => {
3437
expect(testTask.mode).toBe('run')
3538
expect(testTask.location).toMatchObject({
3639
line: 4,
37-
column: 31, // TODO: should it be 5 instead?
40+
column: variableFixture === fixture ? 38 : 31, // TODO: should it be 5 instead? since we only care about "line", ignore for now
3841
})
3942

4043
expect(suiteTask.name).toBe('Drafts')
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Vitest } from './vitest'
2+
3+
Vitest.describe('TicketDetailBottomBar', () => {
4+
Vitest.it.each(['submit', 'discard'])(
5+
'emits %s event when button is clicked',
6+
async (eventName) => {
7+
},
8+
)
9+
10+
Vitest.describe('Drafts', () => {
11+
Vitest.it.todo('should not display draft information if ticket has no draft')
12+
Vitest.it.todo('should display draft information if ticket has a draft')
13+
})
14+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as Vitest from 'vitest'

0 commit comments

Comments
 (0)