vitest-dev / vitest-dev/vitest
v5 browser mode no longer serves /mockServiceWorker.js (undocumented)
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
- Vitest 4.1 browser project with
setupWorker()frommsw/browserin a setup file. Nopublic/mockServiceWorker.js. worker.start()succeeds.- Upgrade to 5.0.0. Do not add a public worker file.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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