vitest icon indicating copy to clipboard operation
vitest copied to clipboard

`vi.mock` stops working in browser mode with msw

Open UnsungHero97 opened this issue 1 month ago • 3 comments

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:

  1. with firefox or webkit configured, run test a few times - all good
  2. run test:watch - all good
  3. hit enter in the terminal - test fails
  4. make a simple change in the test file and save to trigger compilation - test still fails
  5. kill test:watch
  6. comment out the msw stuff
  7. run test:watch again - all good
  8. hit enter in the terminal - all good, tests pass
  9. 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

UnsungHero97 avatar Oct 17 '26 23:10 UnsungHero97

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.

nomadical avatar Oct 26 '26 17:10 nomadical

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.

sheremet-va avatar Nov 03 '26 14:11 sheremet-va

Confirmed, v5.1.0 fixes this issue!

UnsungHero97 avatar Nov 03 '26 22:11 UnsungHero97