Skip to content

Commit f50ea7a

Browse files
authored
fix(browser): ensure setup files are re-evaluated on each test run (fixes #8883) (#8884)
1 parent 31706df commit f50ea7a

File tree

7 files changed

+57
-1
lines changed

7 files changed

+57
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ export function createBrowserRunner(
257257

258258
importFile = async (filepath: string, mode: 'collect' | 'setup') => {
259259
let hash = this.hashMap.get(filepath)
260-
if (!hash) {
260+
261+
// if the mode is setup, we need to re-evaluate the setup file on each test run
262+
if (mode === 'setup' || !hash) {
261263
hash = Date.now().toString()
262264
this.hashMap.set(filepath, hash)
263265
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from 'vitest'
2+
import { counter } from './counter'
3+
4+
test('increment counter', () => {
5+
counter.increment()
6+
expect(counter.get()).toBe(1)
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from 'vitest'
2+
import { counter } from './counter'
3+
4+
test('make sure the counter is reset by the setup file beforeEach hook', () => {
5+
counter.increment()
6+
expect(counter.get()).toBe(1)
7+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { beforeEach } from 'vitest'
2+
import { counter } from './counter'
3+
4+
beforeEach(() => counter.reset())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const g = globalThis as unknown as { counter: number };
2+
export const counter = {
3+
get: () => g.counter,
4+
increment: () => g.counter++,
5+
reset: () => (g.counter = 0),
6+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { fileURLToPath } from 'node:url'
2+
import { defineConfig } from 'vitest/config'
3+
import { instances, provider } from '../../settings'
4+
5+
export default defineConfig({
6+
cacheDir: fileURLToPath(new URL("./node_modules/.vite", import.meta.url)),
7+
test: {
8+
setupFiles: ['./browser-setup.ts'],
9+
browser: {
10+
enabled: true,
11+
isolate: false,
12+
provider,
13+
instances,
14+
headless: false,
15+
},
16+
},
17+
})

test/browser/specs/runner.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,19 @@ test('in-source tests run correctly when filtered', async () => {
290290
expect(stdout).toContain(`Tests ${instances.length} passed`)
291291
})
292292

293+
test('re-evaluate setupFiles on each test run even when isolate is false', async () => {
294+
const { exitCode, stderr, stdout } = await runBrowserTests({
295+
root: './fixtures/isolate-and-setup-file',
296+
})
297+
298+
expect(stderr).toBe('')
299+
expect(exitCode).toBe(0)
300+
instances.forEach(({ browser }) => {
301+
expect(stdout).toReportPassedTest('a.test.ts', browser)
302+
expect(stdout).toReportPassedTest('b.test.ts', browser)
303+
})
304+
})
305+
293306
test.runIf(provider.name === 'playwright')('timeout hooks', async ({ onTestFailed }) => {
294307
const { stderr } = await runBrowserTests({
295308
root: './fixtures/timeout-hooks',

0 commit comments

Comments
 (0)