`vi.mock` stops working in browser mode with msw
Describe the bug
I'm trying to write tests to run in browser mode with msw for mocking requests. Initially, vi.mock works on the first run in --watch mode, but on subsequent runs, it seems something changes under the hood and the mocks are no longer having an effect.
This is happening with firefox and webkit browser instances; chromium seems ok.
There is an issue from 2 years ago that seems like the exact same issue. Its marked as fixed, not sure if that issue is back or if this is a new, similar but unrelated issue. Also, I'm not sure if this is an issue with msw or vitest so I posted in msw as well:
https://github.com/mswjs/msw/issues/2820
Reproduction
Here's a repo for reproducing the issue:
https://github.com/UnsungHero97/msw-vitest-browser-mode-mocking-issue
Steps:
- with
firefoxorwebkitconfigured, runtesta few times - all good - run
test:watch- all good - hit enter in the terminal - test fails
- make a simple change in the test file and save to trigger compilation - test still fails
- kill
test:watch - comment out the
mswstuff - run
test:watchagain - all good - hit enter in the terminal - all good, tests pass
- update the test file to trigger compilation - all good, tests pass
Switch the browser instance to chromium and try again. With chromium, all good, tests pass, cannot reproduce.
Here's a screen recording of the issue:
https://github.com/user-attachments/assets/739728ef-c45b-4445-87f9-87a2526babbf
System Info
System:
OS: macOS 26.6
CPU: (10) arm64 Apple M1 Pro
Memory: 303.17 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 24.19.0 - /Users/hristo/.local/state/fnm_multishells/53992_1792279819422/bin/node
npm: 11.17.0 - /Users/hristo/.local/state/fnm_multishells/53992_1792279819422/bin/npm
pnpm: 11.20.0 - /Users/hristo/.local/state/fnm_multishells/53992_1792279819422/bin/pnpm
Browsers:
Chrome: 155.0.8059.85
Firefox: 158.0
Safari: 26.6
Used Package Manager
npm
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
I can reproduce this with vitest 5.0.5. In WebKit and Firefox, every rerun after the first loads the real module. Chromium is fine. It only happens once network.enable() registers msw's service worker.
It no longer reproduces on main since #11534, which serves mocks through an import map by default, so 5.1 should fix it. With browser.importMapMocks: false it still fails on main, though. Once the service worker controls the page, module requests go through it, and Playwright's context.route() only sees service-worker requests in Chromium (the experimental PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS flag).
Minimal repro: msw/vite plugin, await network.enable() in beforeAll, and a vi.mock factory. Run in watch mode and press Enter.
Please, check if 5.1.0 fixes this for you. Vitest no longer intercepts requests, we use import maps now. Disabling browser.importMapMocks should bring the bug back.
Confirmed, v5.1.0 fixes this issue!