From 04df286ee3e88db328148122face9e4a01fee084 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 1 May 2024 18:52:20 +0900 Subject: [PATCH] fix: delay initializeDistPath + test createVitest --- packages/vitest/src/node/core.ts | 17 +++++++---- test/cli/fixtures/create-vitest/basic.test.ts | 5 ++++ .../fixtures/create-vitest/vitest.config.ts | 3 ++ test/cli/test/create-vitest.test.ts | 28 +++++++++++++++++++ 4 files changed, 48 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/test/create-vitest.test.ts diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 685c1c4b5a6d..0b97619c4a80 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -381,11 +381,6 @@ export class Vitest { async start(filters?: string[]) { this._onClose = [] - // 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') - try { await this.initCoverageProvider() await this.coverageProvider?.clean(this.config.coverage.clean) @@ -508,7 +503,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/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', + }, + }, + ], + [], + ]) +})