Skip to content

Browser mode: Test files are no longer reloading between runs when using the programmatic API #7957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
nicojs opened this issue May 10, 2025 · 2 comments
Closed
6 tasks done

Comments

@nicojs
Copy link
Contributor

nicojs commented May 10, 2025

Describe the bug

When using the programmatic API, the setup files are no longer reloaded between runs using browser mode. This behavior seems to have been broken since vitest 2.

Note: this behavior is vital for StrykerJS to work.

Reproduction

See https://github.com/nicojs/vitest-browser-reload-file

The repo has 1 test file that verifies the globalThis.id variable:

import { expect, test } from "vitest";

test("global id to be 1", () => {
  expect(globalThis.id).toBe("1");
});
test("global id to be 2", () => {
  expect(globalThis.id).toBe("2");
});

The main.ts file here uses createVitest to create a vitest instance and writes a setup file that switches the value of globalThis.id:

await fs.writeFile(setupFile, "globalThis.id = '1';");

// And later
await fs.writeFile(setupFile, "globalThis.id = '2';");

Between it will run the vitest (and reset the context). It should log:

['global id to be 1: pass', 'global id to be 2: fail']
['global id to be 1: fail', 'global id to be 2: pass']

But it logs:

['global id to be 1: pass', 'global id to be 2: fail']
['global id to be 1: pass', 'global id to be 2: fail']

This proves the setup file isn't reloaded between runs.

  • Disabling browser mode will solve this
  • Downgrading to vitest 1 will solve this: npm i -D vitest@1 @vitest/browser@1.

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz
    Memory: 8.67 GB / 15.49 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 22.13.0 - ~/.nvm/versions/node/v22.13.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.13.0/bin/npm
  Browsers:
    Chrome: 133.0.6943.53
  npmPackages:
    @vitest/browser: ^3.1.3 => 3.1.3 
    playwright: ^1.52.0 => 1.52.0 
    vitest: ^3.1.3 => 3.1.3

Used Package Manager

npm

Validations

@nicojs
Copy link
Contributor Author

nicojs commented May 10, 2025

I did find that you've been supporting custom commands since vite@2. I think we can leverage that instead of the file communication magic with rewriting the setup file between runs 👍

EDIT: Do we have something similar for node? To communicate between the test environment and 'the server'?

@nicojs
Copy link
Contributor Author

nicojs commented May 10, 2025

Even better! There is a provide api for providing stuff to test environments. This seems to work beautify for browser mode and 'normal mode'

I'll rewrite Stryker's inner workings this provide API. This is much cleaner than this file-rewriting. No need to fix this edge case of an edge case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant