vitest-dev / vitest-dev/vitest

v5 browser mode no longer serves /mockServiceWorker.js (undocumented)

Open
#11,230 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

Vitest 4.1 browser mode served MSW's worker at /mockServiceWorker.js via the vitest:browser:resolve-virtual plugin. setupWorker().start() worked with no public/ file.

That plugin is gone in 5.0.0. Same start() now 404s:

[MSW] Failed to register a Service Worker for scope ('http://localhost:…/') with script ('http://localhost:…/mockServiceWorker.js'): Service Worker script does not exist at the given path.

Did you forget to run "npx msw init <PUBLIC_DIR>"?

The v5 migration guide does not mention this. I only found it by reading the plugin source after the tests failed.

This looks like leftover fallout from #7556 / #7576 (stop bundling MSW, mock modules with page.route). Fair change. The resolve was still in 4.1.10 though, so anyone who relied on it hits a wall on 5.0 with no note in the guide.

4.1.10 still has the virtual resolve:

https://github.com/vitest-dev/vitest/blob/v4.1.10/packages/browser/src/node/plugin.ts#L313-L320

{
  name: 'vitest:browser:resolve-virtual',
  async resolveId(rawId) {
    if (rawId === '/mockServiceWorker.js') {
      return this.resolve('msw/mockServiceWorker.js', distRoot, {
        skipSelf: true,
      })
    }
  },
},

5.0.0's plugin list does not.

I am not asking to bring MSW back as a Vitest dependency. Either restore a tiny resolve that maps /mockServiceWorker.js to the project's msw package, or say in the migration guide that browser-mode setupWorker() now needs msw init public.

Reproduction
  1. Vitest 4.1 browser project with setupWorker() from msw/browser in a setup file. No public/mockServiceWorker.js.
  2. worker.start() succeeds.
  3. Upgrade to 5.0.0. Do not add a public worker file.
  4. worker.start() throws the MSW "script does not exist" error.

Workaround that unblocked us: msw init public --save and commit the generated file.

System Info
vitest: 5.0.0 (broke)
vitest: 4.1.10 (worked)
@vitest/browser-playwright: 5.0.0
msw: 2.15.0 (unchanged across the upgrade)
Used Package Manager

pnpm

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the browser plugin in packages/browser/src/node/plugin.ts at v4.1.10 with the v5.0.0 plugin list, then read the v5 migration guide entry for browser mode. Determine whether the missing /mockServiceWorker.js behavior should be restored or documented, and verify that either the worker loads without a public file or the migration guidance clearly explains the required msw init workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
playwright, typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.