Skip to content

Commit

Permalink
fix(browser): don't add v= queries to setup files imports (#6759)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Oct 21, 2024
1 parent ec48898 commit b82584c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/browser/src/client/tester/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface BrowserRunnerOptions {

export const browserHashMap = new Map<
string,
[test: boolean, timstamp: string]
string
>()

interface CoverageHandler {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/tester/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand Down
6 changes: 6 additions & 0 deletions test/browser/fixtures/setup-file/browser-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { beforeEach } from 'vitest';
import * as source from './source';

beforeEach<{ source: any }>((t) => {
t.source = source
})
6 changes: 6 additions & 0 deletions test/browser/fixtures/setup-file/module-equality.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
3 changes: 3 additions & 0 deletions test/browser/fixtures/setup-file/source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function answer() {
return 42
}
19 changes: 19 additions & 0 deletions test/browser/fixtures/setup-file/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
1 change: 1 addition & 0 deletions test/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions test/browser/specs/setup-file.test.ts
Original file line number Diff line number Diff line change
@@ -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",
}
`)
})
2 changes: 1 addition & 1 deletion test/browser/specs/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit b82584c

Please sign in to comment.