From f5faf4236f6af146647a81179ccbef73bdb5c965 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 2 May 2024 17:15:44 +0900 Subject: [PATCH] fix: call `resolveId('vitest')` after `buildStart` (#5646) --- packages/vitest/src/node/core.ts | 18 +++++--- test/cli/fixtures/create-vitest/basic.test.ts | 5 +++ .../fixtures/create-vitest/vitest.config.ts | 3 ++ test/cli/fixtures/plugin/basic.test.ts | 5 +++ test/cli/fixtures/plugin/vitest.config.ts | 45 +++++++++++++++++++ test/cli/test/create-vitest.test.ts | 28 ++++++++++++ test/cli/test/plugin.test.ts | 15 +++++++ 7 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 test/cli/fixtures/create-vitest/basic.test.ts create mode 100644 test/cli/fixtures/create-vitest/vitest.config.ts create mode 100644 test/cli/fixtures/plugin/basic.test.ts create mode 100644 test/cli/fixtures/plugin/vitest.config.ts create mode 100644 test/cli/test/create-vitest.test.ts create mode 100644 test/cli/test/plugin.test.ts diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 4cfed6786c2f..2ca7acc68c4a 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -95,6 +95,7 @@ export class Vitest { this.pool = undefined this.coverageProvider = undefined this.runningPromise = undefined + this.distPath = undefined! this.projectsTestFiles.clear() const resolved = resolveConfig(this.mode, options, server.config, this.logger) @@ -110,11 +111,6 @@ export class Vitest { this.vitenode = new ViteNodeServer(server, this.config.server) - // if Vitest is running globally, then we should still import local vitest if possible - const projectVitestPath = await this.vitenode.resolveId('vitest') - const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir - this.distPath = join(vitestDir, 'dist') - const node = this.vitenode this.runner = new ViteNodeRunner({ root: server.config.root, @@ -508,7 +504,19 @@ export class Vitest { await project.initializeGlobalSetup() } + private async initializeDistPath() { + if (this.distPath) + return + + // if Vitest is running globally, then we should still import local vitest if possible + const projectVitestPath = await this.vitenode.resolveId('vitest') + const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir + this.distPath = join(vitestDir, 'dist') + } + async runFiles(paths: WorkspaceSpec[], allTestsRun: boolean) { + await this.initializeDistPath() + const filepaths = paths.map(([, file]) => file) this.state.collectPaths(filepaths) diff --git a/test/cli/fixtures/create-vitest/basic.test.ts b/test/cli/fixtures/create-vitest/basic.test.ts new file mode 100644 index 000000000000..9bb0283e10c6 --- /dev/null +++ b/test/cli/fixtures/create-vitest/basic.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from "vitest"; + +test("basic", () => { + expect(1).toBe(1); +}) diff --git a/test/cli/fixtures/create-vitest/vitest.config.ts b/test/cli/fixtures/create-vitest/vitest.config.ts new file mode 100644 index 000000000000..abed6b2116e1 --- /dev/null +++ b/test/cli/fixtures/create-vitest/vitest.config.ts @@ -0,0 +1,3 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({}) diff --git a/test/cli/fixtures/plugin/basic.test.ts b/test/cli/fixtures/plugin/basic.test.ts new file mode 100644 index 000000000000..9bb0283e10c6 --- /dev/null +++ b/test/cli/fixtures/plugin/basic.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from "vitest"; + +test("basic", () => { + expect(1).toBe(1); +}) diff --git a/test/cli/fixtures/plugin/vitest.config.ts b/test/cli/fixtures/plugin/vitest.config.ts new file mode 100644 index 000000000000..3659e85fbe47 --- /dev/null +++ b/test/cli/fixtures/plugin/vitest.config.ts @@ -0,0 +1,45 @@ +import { defineConfig } from 'vitest/config' + +function logHook(...args: unknown[]) { + ((globalThis as any).__testHooks ??= []).push(args[0]); + + if (process.env["LOG_HOOK"]) { + console.log(...args); + } +} + +export default defineConfig({ + plugins: [ + { + name: "test-default", + configureServer() { + logHook("configureServer(default)") + }, + buildStart() { + logHook("buildStart(default)") + }, + resolveId(source) { + logHook("resolveId(default)", source) + }, + transform(_code, id) { + logHook("transform(default)", id) + }, + }, + { + name: "test-pre", + enforce: "pre", + configureServer() { + logHook("configureServer(pre)") + }, + buildStart() { + logHook("buildStart(pre)") + }, + resolveId(source) { + logHook("resolveId(pre)", source) + }, + transform(_code, id) { + logHook("transform(pre)", id) + }, + } + ] +}) diff --git a/test/cli/test/create-vitest.test.ts b/test/cli/test/create-vitest.test.ts new file mode 100644 index 000000000000..ff24ea211a43 --- /dev/null +++ b/test/cli/test/create-vitest.test.ts @@ -0,0 +1,28 @@ +import { expect, it, vi } from 'vitest' +import { createVitest } from 'vitest/node' + +it(createVitest, async () => { + const onFinished = vi.fn() + const ctx = await createVitest('test', { + watch: false, + root: 'fixtures/create-vitest', + reporters: [ + { + onFinished, + }, + ], + }) + const testFiles = await ctx.globTestFiles() + await ctx.runFiles(testFiles, false) + expect(onFinished.mock.calls[0]).toMatchObject([ + [ + { + name: 'basic.test.ts', + result: { + state: 'pass', + }, + }, + ], + [], + ]) +}) diff --git a/test/cli/test/plugin.test.ts b/test/cli/test/plugin.test.ts new file mode 100644 index 000000000000..63f72c9a3464 --- /dev/null +++ b/test/cli/test/plugin.test.ts @@ -0,0 +1,15 @@ +import { expect, it } from 'vitest' +import { runVitest } from '../../test-utils' + +it('plugin hooks', async () => { + await runVitest({ root: './fixtures/plugin' }) + expect((globalThis as any).__testHooks.slice(0, 5)).toEqual( + [ + 'configureServer(pre)', + 'configureServer(default)', + 'buildStart(pre)', + 'buildStart(default)', + 'resolveId(pre)', + ], + ) +})