diff --git a/packages/browser/src/client/tester/runner.ts b/packages/browser/src/client/tester/runner.ts index 370046c3acb4..dea0f2645a24 100644 --- a/packages/browser/src/client/tester/runner.ts +++ b/packages/browser/src/client/tester/runner.ts @@ -17,7 +17,7 @@ interface BrowserRunnerOptions { export const browserHashMap = new Map< string, - [test: boolean, timstamp: string] + string >() interface CoverageHandler { @@ -122,15 +122,15 @@ export function createBrowserRunner( } importFile = async (filepath: string) => { - let [test, hash] = this.hashMap.get(filepath) ?? [false, ''] - if (hash === '') { + let hash = this.hashMap.get(filepath) + if (!hash) { hash = Date.now().toString() - this.hashMap.set(filepath, [false, hash]) + this.hashMap.set(filepath, hash) } // on Windows we need the unit to resolve the test file const prefix = `/${/^\w:/.test(filepath) ? '@fs/' : ''}` - const query = `${test ? 'browserv' : 'v'}=${hash}` + const query = `browserv=${hash}` const importpath = `${prefix}${filepath}?${query}`.replace(/\/+/g, '/') await import(/* @vite-ignore */ importpath) } diff --git a/packages/browser/src/client/tester/tester.ts b/packages/browser/src/client/tester/tester.ts index 3bdc80e09fd5..37a582ce887d 100644 --- a/packages/browser/src/client/tester/tester.ts +++ b/packages/browser/src/client/tester/tester.ts @@ -75,7 +75,7 @@ async function prepareTestEnvironment(files: string[]) { files.forEach((filename) => { const currentVersion = browserHashMap.get(filename) if (!currentVersion || currentVersion[1] !== version) { - browserHashMap.set(filename, [true, version]) + browserHashMap.set(filename, version) } }) diff --git a/test/browser/fixtures/setup-file/browser-setup.ts b/test/browser/fixtures/setup-file/browser-setup.ts new file mode 100644 index 000000000000..736120c2a4b5 --- /dev/null +++ b/test/browser/fixtures/setup-file/browser-setup.ts @@ -0,0 +1,6 @@ +import { beforeEach } from 'vitest'; +import * as source from './source'; + +beforeEach<{ source: any }>((t) => { + t.source = source +}) diff --git a/test/browser/fixtures/setup-file/module-equality.test.ts b/test/browser/fixtures/setup-file/module-equality.test.ts new file mode 100644 index 000000000000..7b675256d51a --- /dev/null +++ b/test/browser/fixtures/setup-file/module-equality.test.ts @@ -0,0 +1,6 @@ +import { expect, test } from 'vitest' +import * as source from './source' + +test<{ source: any }>('modules are the same', (t) => { + expect(source).toBe(t.source) +}) diff --git a/test/browser/fixtures/setup-file/source.ts b/test/browser/fixtures/setup-file/source.ts new file mode 100644 index 000000000000..00589e6d4f4a --- /dev/null +++ b/test/browser/fixtures/setup-file/source.ts @@ -0,0 +1,3 @@ +export function answer() { + return 42 +} diff --git a/test/browser/fixtures/setup-file/vitest.config.ts b/test/browser/fixtures/setup-file/vitest.config.ts new file mode 100644 index 000000000000..8172e44773a7 --- /dev/null +++ b/test/browser/fixtures/setup-file/vitest.config.ts @@ -0,0 +1,19 @@ +import { fileURLToPath } from 'node:url' +import { defineConfig } from 'vitest/config' + +const provider = process.env.PROVIDER || 'playwright' +const name = + process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome') + +export default defineConfig({ + cacheDir: fileURLToPath(new URL("./node_modules/.vite", import.meta.url)), + test: { + setupFiles: ['./browser-setup.ts'], + browser: { + enabled: true, + provider, + name, + headless: false, + }, + }, +}) diff --git a/test/browser/package.json b/test/browser/package.json index a0ff07b90101..608dacdae7c0 100644 --- a/test/browser/package.json +++ b/test/browser/package.json @@ -12,6 +12,7 @@ "test-mocking": "vitest --root ./fixtures/mocking", "test-mocking-watch": "vitest --root ./fixtures/mocking-watch", "test-locators": "vitest --root ./fixtures/locators", + "test-setup-file": "vitest --root ./fixtures/setup-file", "test-snapshots": "vitest --root ./fixtures/update-snapshot", "coverage": "vitest --coverage.enabled --coverage.provider=istanbul --browser.headless=yes", "test:browser:preview": "PROVIDER=preview vitest", diff --git a/test/browser/specs/setup-file.test.ts b/test/browser/specs/setup-file.test.ts new file mode 100644 index 000000000000..bdf97071bbe5 --- /dev/null +++ b/test/browser/specs/setup-file.test.ts @@ -0,0 +1,23 @@ +// fix https://github.com/vitest-dev/vitest/issues/6690 + +import { expect, test } from 'vitest' +import { runBrowserTests } from './utils' + +test('setup file imports the same modules', async () => { + const { stderr, ctx } = await runBrowserTests( + { + root: './fixtures/setup-file', + }, + ) + + expect(stderr).toBe('') + expect( + Object.fromEntries( + ctx.state.getFiles().map(f => [f.name, f.result.state]), + ), + ).toMatchInlineSnapshot(` + { + "module-equality.test.ts": "pass", + } + `) +}) diff --git a/test/browser/specs/utils.ts b/test/browser/specs/utils.ts index 944d1492f61f..2b29e55d7a69 100644 --- a/test/browser/specs/utils.ts +++ b/test/browser/specs/utils.ts @@ -1,5 +1,5 @@ import type { UserConfig as ViteUserConfig } from 'vite' -import type { UserConfig } from 'vitest' +import type { UserConfig } from 'vitest/node' import { runVitest } from '../../test-utils' export const provider = process.env.PROVIDER || 'playwright'