From 77bf0dbbfbd5e374e05df982ec1e265c36b84a21 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 20 Oct 2024 17:51:58 +0900 Subject: [PATCH 1/2] fix(vitest): resolve setupFiles before invalidate --- packages/vitest/src/runtime/runners/benchmark.ts | 5 +++-- packages/vitest/src/runtime/runners/test.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/runtime/runners/benchmark.ts b/packages/vitest/src/runtime/runners/benchmark.ts index 1bf002f19466..25a8342e88ac 100644 --- a/packages/vitest/src/runtime/runners/benchmark.ts +++ b/packages/vitest/src/runtime/runners/benchmark.ts @@ -165,9 +165,10 @@ export class NodeBenchmarkRunner implements VitestRunner { return await import('tinybench') } - importFile(filepath: string, source: VitestRunnerImportSource): unknown { + async importFile(filepath: string, source: VitestRunnerImportSource): Promise { if (source === 'setup') { - getWorkerState().moduleCache.delete(filepath) + const resolved = await this.__vitest_executor.resolveUrl(filepath) + getWorkerState().moduleCache.delete(resolved[1]) } return this.__vitest_executor.executeId(filepath) } diff --git a/packages/vitest/src/runtime/runners/test.ts b/packages/vitest/src/runtime/runners/test.ts index a89a58837d8f..fd39b72ac492 100644 --- a/packages/vitest/src/runtime/runners/test.ts +++ b/packages/vitest/src/runtime/runners/test.ts @@ -33,9 +33,10 @@ export class VitestTestRunner implements VitestRunner { constructor(public config: SerializedConfig) {} - importFile(filepath: string, source: VitestRunnerImportSource): unknown { + async importFile(filepath: string, source: VitestRunnerImportSource): Promise { if (source === 'setup') { - this.workerState.moduleCache.delete(filepath) + const resolved = await this.__vitest_executor.resolveUrl(filepath) + this.workerState.moduleCache.delete(resolved[1]) } return this.__vitest_executor.executeId(filepath) } From 6d645530ae26a9aaa28afa015715c4d895c6f9ef Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 20 Oct 2024 18:02:47 +0900 Subject: [PATCH 2/2] test: add test --- test/config/fixtures/setup-file-no-isolate/setup.ts | 9 +++++++++ .../fixtures/setup-file-no-isolate/vite.config.ts | 9 +++++++++ test/config/fixtures/setup-file-no-isolate/x.test.ts | 6 ++++++ test/config/fixtures/setup-file-no-isolate/y.test.ts | 6 ++++++ test/config/test/setup-file-no-isolate.test.ts | 11 +++++++++++ 5 files changed, 41 insertions(+) create mode 100644 test/config/fixtures/setup-file-no-isolate/setup.ts create mode 100644 test/config/fixtures/setup-file-no-isolate/vite.config.ts create mode 100644 test/config/fixtures/setup-file-no-isolate/x.test.ts create mode 100644 test/config/fixtures/setup-file-no-isolate/y.test.ts create mode 100644 test/config/test/setup-file-no-isolate.test.ts diff --git a/test/config/fixtures/setup-file-no-isolate/setup.ts b/test/config/fixtures/setup-file-no-isolate/setup.ts new file mode 100644 index 000000000000..7ec41a98979b --- /dev/null +++ b/test/config/fixtures/setup-file-no-isolate/setup.ts @@ -0,0 +1,9 @@ +import { beforeEach } from 'vitest'; + +export type MyContext = { + testOk: boolean +} + +beforeEach((ctx) => { + ctx.testOk = true; +}); diff --git a/test/config/fixtures/setup-file-no-isolate/vite.config.ts b/test/config/fixtures/setup-file-no-isolate/vite.config.ts new file mode 100644 index 000000000000..08dec793b3f7 --- /dev/null +++ b/test/config/fixtures/setup-file-no-isolate/vite.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + setupFiles: ['./setup'], + isolate: false, + fileParallelism: false, + }, +}); diff --git a/test/config/fixtures/setup-file-no-isolate/x.test.ts b/test/config/fixtures/setup-file-no-isolate/x.test.ts new file mode 100644 index 000000000000..e4620390b0d3 --- /dev/null +++ b/test/config/fixtures/setup-file-no-isolate/x.test.ts @@ -0,0 +1,6 @@ +import { expect, test } from "vitest" +import type { MyContext } from "./setup" + +test("x", (ctx) => { + expect(ctx.testOk).toBe(true) +}) diff --git a/test/config/fixtures/setup-file-no-isolate/y.test.ts b/test/config/fixtures/setup-file-no-isolate/y.test.ts new file mode 100644 index 000000000000..d2e6fd0c95e3 --- /dev/null +++ b/test/config/fixtures/setup-file-no-isolate/y.test.ts @@ -0,0 +1,6 @@ +import { expect, test } from "vitest" +import type { MyContext } from "./setup" + +test("y", (ctx) => { + expect(ctx.testOk).toBe(true) +}) diff --git a/test/config/test/setup-file-no-isolate.test.ts b/test/config/test/setup-file-no-isolate.test.ts new file mode 100644 index 000000000000..f66943c2dde9 --- /dev/null +++ b/test/config/test/setup-file-no-isolate.test.ts @@ -0,0 +1,11 @@ +import path from 'node:path' +import { expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +test('re-import setupFiles with no isolate', async () => { + const root = path.resolve(import.meta.dirname, '../fixtures/setup-file-no-isolate') + + const { stderr, exitCode } = await runVitest({ root }) + expect(stderr).toBe('') + expect(exitCode).toBe(0) +})