Skip to content

Commit f5faf42

Browse files
authored
fix: call resolveId('vitest') after buildStart (#5646)
1 parent d8304bb commit f5faf42

File tree

7 files changed

+114
-5
lines changed

7 files changed

+114
-5
lines changed

packages/vitest/src/node/core.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class Vitest {
9595
this.pool = undefined
9696
this.coverageProvider = undefined
9797
this.runningPromise = undefined
98+
this.distPath = undefined!
9899
this.projectsTestFiles.clear()
99100

100101
const resolved = resolveConfig(this.mode, options, server.config, this.logger)
@@ -110,11 +111,6 @@ export class Vitest {
110111

111112
this.vitenode = new ViteNodeServer(server, this.config.server)
112113

113-
// if Vitest is running globally, then we should still import local vitest if possible
114-
const projectVitestPath = await this.vitenode.resolveId('vitest')
115-
const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir
116-
this.distPath = join(vitestDir, 'dist')
117-
118114
const node = this.vitenode
119115
this.runner = new ViteNodeRunner({
120116
root: server.config.root,
@@ -508,7 +504,19 @@ export class Vitest {
508504
await project.initializeGlobalSetup()
509505
}
510506

507+
private async initializeDistPath() {
508+
if (this.distPath)
509+
return
510+
511+
// if Vitest is running globally, then we should still import local vitest if possible
512+
const projectVitestPath = await this.vitenode.resolveId('vitest')
513+
const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir
514+
this.distPath = join(vitestDir, 'dist')
515+
}
516+
511517
async runFiles(paths: WorkspaceSpec[], allTestsRun: boolean) {
518+
await this.initializeDistPath()
519+
512520
const filepaths = paths.map(([, file]) => file)
513521
this.state.collectPaths(filepaths)
514522

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from "vitest";
2+
3+
test("basic", () => {
4+
expect(1).toBe(1);
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from "vitest";
2+
3+
test("basic", () => {
4+
expect(1).toBe(1);
5+
})
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
function logHook(...args: unknown[]) {
4+
((globalThis as any).__testHooks ??= []).push(args[0]);
5+
6+
if (process.env["LOG_HOOK"]) {
7+
console.log(...args);
8+
}
9+
}
10+
11+
export default defineConfig({
12+
plugins: [
13+
{
14+
name: "test-default",
15+
configureServer() {
16+
logHook("configureServer(default)")
17+
},
18+
buildStart() {
19+
logHook("buildStart(default)")
20+
},
21+
resolveId(source) {
22+
logHook("resolveId(default)", source)
23+
},
24+
transform(_code, id) {
25+
logHook("transform(default)", id)
26+
},
27+
},
28+
{
29+
name: "test-pre",
30+
enforce: "pre",
31+
configureServer() {
32+
logHook("configureServer(pre)")
33+
},
34+
buildStart() {
35+
logHook("buildStart(pre)")
36+
},
37+
resolveId(source) {
38+
logHook("resolveId(pre)", source)
39+
},
40+
transform(_code, id) {
41+
logHook("transform(pre)", id)
42+
},
43+
}
44+
]
45+
})

test/cli/test/create-vitest.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, it, vi } from 'vitest'
2+
import { createVitest } from 'vitest/node'
3+
4+
it(createVitest, async () => {
5+
const onFinished = vi.fn()
6+
const ctx = await createVitest('test', {
7+
watch: false,
8+
root: 'fixtures/create-vitest',
9+
reporters: [
10+
{
11+
onFinished,
12+
},
13+
],
14+
})
15+
const testFiles = await ctx.globTestFiles()
16+
await ctx.runFiles(testFiles, false)
17+
expect(onFinished.mock.calls[0]).toMatchObject([
18+
[
19+
{
20+
name: 'basic.test.ts',
21+
result: {
22+
state: 'pass',
23+
},
24+
},
25+
],
26+
[],
27+
])
28+
})

test/cli/test/plugin.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, it } from 'vitest'
2+
import { runVitest } from '../../test-utils'
3+
4+
it('plugin hooks', async () => {
5+
await runVitest({ root: './fixtures/plugin' })
6+
expect((globalThis as any).__testHooks.slice(0, 5)).toEqual(
7+
[
8+
'configureServer(pre)',
9+
'configureServer(default)',
10+
'buildStart(pre)',
11+
'buildStart(default)',
12+
'resolveId(pre)',
13+
],
14+
)
15+
})

0 commit comments

Comments
 (0)