GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(browser): ensure setup files are re-evaluated on each test run (f… · vitest-dev/vitest@f50ea7a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/browser/src/client/tester
    • runner.ts
  • test/browser
    • fixtures/isolate-and-setup-file
      • a.test.ts
      • b.test.ts
      • browser-setup.ts
      • counter.ts
      • vitest.config.ts
    • specs
      • runner.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: packages/browser/src/client/tester/runner.ts
+3-1Lines 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
}
Collapse file

‎test/browser/fixtures/isolate-and-setup-file/a.test.ts‎

Copy file name to clipboard
+7Lines 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+
});
Collapse file

‎test/browser/fixtures/isolate-and-setup-file/b.test.ts‎

Copy file name to clipboard
+7Lines 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+
});
Collapse file

‎test/browser/fixtures/isolate-and-setup-file/browser-setup.ts‎

Copy file name to clipboard
+4Lines 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())
Collapse file

‎test/browser/fixtures/isolate-and-setup-file/counter.ts‎

Copy file name to clipboard
+6Lines 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+
};
Collapse file

‎test/browser/fixtures/isolate-and-setup-file/vitest.config.ts‎

Copy file name to clipboard
+17Lines 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+
})
Collapse file

‎test/browser/specs/runner.test.ts‎

Copy file name to clipboardExpand all lines: test/browser/specs/runner.test.ts
+13Lines 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 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.