Skip to content

Commit b82584c

Browse files
authoredOct 21, 2024··
fix(browser): don't add v= queries to setup files imports (#6759)
1 parent ec48898 commit b82584c

File tree

9 files changed

+65
-7
lines changed

9 files changed

+65
-7
lines changed
 

‎packages/browser/src/client/tester/runner.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface BrowserRunnerOptions {
1717

1818
export const browserHashMap = new Map<
1919
string,
20-
[test: boolean, timstamp: string]
20+
string
2121
>()
2222

2323
interface CoverageHandler {
@@ -122,15 +122,15 @@ export function createBrowserRunner(
122122
}
123123

124124
importFile = async (filepath: string) => {
125-
let [test, hash] = this.hashMap.get(filepath) ?? [false, '']
126-
if (hash === '') {
125+
let hash = this.hashMap.get(filepath)
126+
if (!hash) {
127127
hash = Date.now().toString()
128-
this.hashMap.set(filepath, [false, hash])
128+
this.hashMap.set(filepath, hash)
129129
}
130130

131131
// on Windows we need the unit to resolve the test file
132132
const prefix = `/${/^\w:/.test(filepath) ? '@fs/' : ''}`
133-
const query = `${test ? 'browserv' : 'v'}=${hash}`
133+
const query = `browserv=${hash}`
134134
const importpath = `${prefix}${filepath}?${query}`.replace(/\/+/g, '/')
135135
await import(/* @vite-ignore */ importpath)
136136
}

‎packages/browser/src/client/tester/tester.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async function prepareTestEnvironment(files: string[]) {
7575
files.forEach((filename) => {
7676
const currentVersion = browserHashMap.get(filename)
7777
if (!currentVersion || currentVersion[1] !== version) {
78-
browserHashMap.set(filename, [true, version])
78+
browserHashMap.set(filename, version)
7979
}
8080
})
8181

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { beforeEach } from 'vitest';
2+
import * as source from './source';
3+
4+
beforeEach<{ source: any }>((t) => {
5+
t.source = source
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from 'vitest'
2+
import * as source from './source'
3+
4+
test<{ source: any }>('modules are the same', (t) => {
5+
expect(source).toBe(t.source)
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function answer() {
2+
return 42
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { fileURLToPath } from 'node:url'
2+
import { defineConfig } from 'vitest/config'
3+
4+
const provider = process.env.PROVIDER || 'playwright'
5+
const name =
6+
process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome')
7+
8+
export default defineConfig({
9+
cacheDir: fileURLToPath(new URL("./node_modules/.vite", import.meta.url)),
10+
test: {
11+
setupFiles: ['./browser-setup.ts'],
12+
browser: {
13+
enabled: true,
14+
provider,
15+
name,
16+
headless: false,
17+
},
18+
},
19+
})

‎test/browser/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"test-mocking": "vitest --root ./fixtures/mocking",
1313
"test-mocking-watch": "vitest --root ./fixtures/mocking-watch",
1414
"test-locators": "vitest --root ./fixtures/locators",
15+
"test-setup-file": "vitest --root ./fixtures/setup-file",
1516
"test-snapshots": "vitest --root ./fixtures/update-snapshot",
1617
"coverage": "vitest --coverage.enabled --coverage.provider=istanbul --browser.headless=yes",
1718
"test:browser:preview": "PROVIDER=preview vitest",

‎test/browser/specs/setup-file.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// fix https://github.com/vitest-dev/vitest/issues/6690
2+
3+
import { expect, test } from 'vitest'
4+
import { runBrowserTests } from './utils'
5+
6+
test('setup file imports the same modules', async () => {
7+
const { stderr, ctx } = await runBrowserTests(
8+
{
9+
root: './fixtures/setup-file',
10+
},
11+
)
12+
13+
expect(stderr).toBe('')
14+
expect(
15+
Object.fromEntries(
16+
ctx.state.getFiles().map(f => [f.name, f.result.state]),
17+
),
18+
).toMatchInlineSnapshot(`
19+
{
20+
"module-equality.test.ts": "pass",
21+
}
22+
`)
23+
})

‎test/browser/specs/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { UserConfig as ViteUserConfig } from 'vite'
2-
import type { UserConfig } from 'vitest'
2+
import type { UserConfig } from 'vitest/node'
33
import { runVitest } from '../../test-utils'
44

55
export const provider = process.env.PROVIDER || 'playwright'

0 commit comments

Comments
 (0)
Please sign in to comment.