MSW (Mock Service Worker)
MSW's API is documented at mswjs.io/docs, with a debugging runbook and integration guides for Vitest without Vite and Vite; mswjs.io serves no llms.txt. The Vitest page sends Vite projects to the Vite page, which has no Vitest setup; follow the Vitest browser steps below. This skill covers where the Svelte, Storybook and Vitest toolchain departs from those pages.
vi.mock replaces a module and MSW replaces the network. Use MSW to apply svelte-5:testing-svelte's "Mock at the right seam" to HTTP, so the component, fetch, Request and Response stay real. Not for replacing a module (vi.mock, svelte-5:testing-svelte) or for a one-off override in a Playwright test (page.route, see E2E below).
Three surfaces, three handler sets
| Surface | Started in | Handler set |
|---|---|---|
| Storybook | mswLoader() in .storybook/preview.ts |
full: every story must be hermetic |
| Vitest browser | setupWorker in a setup file, once per page |
narrow: per-test worker.use(), plus a guard that fails on unmocked requests |
| Dev app | gated entry import (Vite integration); on SvelteKit, init in hooks.client/hooks.server via virtual:msw |
narrow: only what the backend lacks |
Keep one handler set per surface; one shared array makes dev stop reaching your backend once a story adds a catch-all (handlers). On SvelteKit, start from the @msw/sveltekit add-on rather than hand-wiring hooks; read the code sv will run before running it (a community add-on): sveltekit.
Vitest browser setup
Before step 1: install msw (pnpm add -D msw, or the @msw/sveltekit add-on on SvelteKit), serve the worker script (next section), and create the mocks index module the setup files import (handlers, "One handler set per surface").
Put the
setupWorkerfile in the browser project'ssetupFiles, not the roottest.setupFiles. Inline projects inherit rootsetupFiles(arrays concatenate), so a root entry also reaches node projects, wheremsw/browserdoes not resolve (its export map sets thenodecondition tonull), and in the Vitest storybook project it starts a second worker next tomswLoader's, which fails every story with theWebSocketerror from step 2. A node project that must not inherit the root needsextends: false(svelte-5:vitest)Start one worker per page: keep the promise that
start()returns in aglobalThisslot, and letstart()create and start the worker. msw 3 throws on a second worker (Failed to replace a global value at "WebSocket": already replaced.), and a test that importsworkerfrom the setup file evaluates it a second time. The full file and the reasons are in msw-3const page = globalThis as typeof globalThis & { __msw?: Promise<SetupWorker>; }; page.__msw ??= start(); export const worker = await page.__msw; afterEach(() => worker.resetHandlers());Place the rest of the setup file so it survives a second evaluation of the file and
--isolate=false(reasons in msw-3):- Vitest hooks at module scope, after the slot
worker.eventslisteners insidestart()- no
worker.stop()inafterAll - an
onUnhandledFramecallback rather than plain'warn': the test's own.sveltemodules load through the worker and would each warn (the callback is in the msw-3 setup file; the step 4 guard file replaces it and skips everything outside your API paths) - no
test.extendfixture from msw's Vitest page (written for Vitest without Vite): it callsworker.start()in every test, which msw 3 rejects from the second test on (the worker is already started)
Add the unmocked-request guard from handlers. Its example covers steps 2 to 4 in one file
The worker script
The browser needs the worker script, mockServiceWorker.js, served from the app root. Two ways to serve it:
msw/viteplugin (Vite 6 or later):import { msw } from "msw/vite"and addmsw()to the Vite plugins (the@msw/sveltekitadd-on does this)- it serves the worker script to the dev server and to every Vitest browser project whose config loads the plugin, the Vitest storybook project included; a standalone
vitest.config.tsloads it by listingmsw()in its ownplugins storybook devgets it from the plugin: open/mockServiceWorker.json its origin to confirm, and commit the file (item 2) if it is missing- a
storybook buildnever contains it, because a Storybook preview does not importvirtual:msw(next bullet), so a built Storybook needs the committed file (item 2) - it emits the script only into client bundles that import
virtual:msw, so a DEV guard around that import keeps it out of production. Keep the DEV guard, as the plugin's own doc comment says: "Guard mocking setup withimport.meta.env.DEVto exclude both from production builds" (both: the mocking setup and the worker script) (and the Vite integration page's examples do); a build whosevirtual:mswblock is not DEV-gated ships msw's chunks and the worker script - the default mode also serves the script to code that calls
setupWorkeritself, at the Vitebase(the root by default); with anotherbase, passserviceWorker: { url }to thatstart() msw({ mode: "worker-only" })drops the virtual modules and writes the script into every client build, production included, although the Vite integration page says the worker script is "neither served nor written to your build output" in production
- it serves the worker script to the dev server and to every Vitest browser project whose config loads the plugin, the Vitest storybook project included; a standalone
- Committed file, for a project without the plugin, for a Vitest config that does not load it (a standalone
vitest.config.tswithsvelte()orsveltekit()but nomsw()), for a built Storybook, and forstorybook devwhen the check above fails:- run
pnpm exec msw init <dir> --saveintopublic/on Vite andstatic/on SvelteKit, and re-run it after every msw upgrade: msw 3 has no postinstall step that refreshes the file, although the linked page says--savecopies it on install. A stale file logs "The currently registered Service Worker has been generated by a different version of MSW" storybook dev,storybook buildand the Vitest storybook project all pick it up from Vite'spublicDir(static/on SvelteKit,public/by default), so nostaticDirsentry is needed. For a script outsidepublicDir, addstaticDirsin.storybook/main.tsforstorybook devandstorybook build, and setpublicDirin the Vitest config for the Vitest storybook project (addon-vitest FAQ)- Vite copies
public/"to the root of the dist directory as-is" (Vite assets docs), and SvelteKit does the same withstatic/, so the committed file ships with every production build; only code that registers it would activate it, which the DEV guard keeps out
- run
Storybook wiring for Svelte CSF
Baseline handlers go in setupWorker(...) inside mswLoader(...) from msw-storybook-addon/csf3, never in preview.parameters.msw: the loader resets to the setupWorker set before each story, and a story's own parameters.msw ([...] or { handlers: [...] }) replaces a project-level one; only the named-Record form merges by key. Write new per-story overrides as beforeEach={({ msw }) => msw.use(...)} on <Story>; existing parameters.msw overrides keep working on the mswLoader() path. The preview.ts snippet and the override details are in storybook.
Traps
- msw 3 reads
onUnhandledFrameand ignoresonUnhandledRequest, so a leftoveronUnhandledRequest: 'error'falls back to'warn', with no message about the ignored key. TypeScript flags the old key in an object literal passed tostart()orlisten()in a file your tsconfig includes (on ansv createSvelteKit project,.storybook/andtests/are not), while an options object built elsewhere can carry it unflagged; JavaScript and.sveltefiles without type checks do not flag it. A custom callback takes({ frame, defaults }):frame.protocol === 'http'marks a request frame,frame.data.requestholds the request, anddefaults.warn()prints the default warning onUnhandledFrame: 'error'fails no browser test: MSW logs the request and answers it with a 500 itself, so a component that handles the failure stays green. Record the request in a callback and assert inafterEach: handlers- The built-in
'warn'and'error'skip every request whose path ends in a common file extension,.json,.txt,.csv,.xml,.mdand.pdfincluded: an unmockedGET /api/items.jsongets no warning and no 500 and reaches the network. Guard API paths by prefix instead (handlers, list in msw-3) - Listing
msw/browserbut notmsw/experimentalinoptimizeDeps.includeloads two copies of msw's classes, soframe instanceof HttpNetworkFrameisfalsefor every frame: once you list one msw entry point, list every one you import, or checkframe.protocol.svelte-5:storybook-vitestownsoptimizeDepsfor the Vitest storybook project - A string path ignores the query:
http.get('/api/items', ...)also answers/api/items?page=2, and a?in the path (http.get('/api/items?page=1', ...)) is dropped withFound a redundant usage of query parameters; match the query with a predicate (handlers) - A matched handler whose resolver returns nothing does not make the request unhandled. MSW falls through to the next matching handler and, if none answers, sends the request to the network;
onUnhandledFramenever sees it. To leave other queries unhandled, match only the query you answer: handlers has a predicate handler for that - msw 3 intercepts navigations, including iframe loads (msw#2721): a same-origin
<iframe>load goes through the worker like afetch, so give the framed URL a handler or it is handled as an unmocked request (a warning, or a failure under the guard). A cross-origin frame document never reaches the page's worker; cross-originfetchcalls from the page still do - Handler modules must be import-safe. Warn on a missing fixture, never throw: Vitest tags skip a test body but still import the file (
svelte-5:vitest) - Run the Vitest storybook project with
--silent(svelte-5:storybook-vitest), or console output buries the errors (msw logs every mocked request, plus a start message, when the setup leaves outquiet: true).--silentalso hides the console warnings that name each unhandled request. In an AI agent's run, Vitest hides them for passing stories even without--silentwhen neither the config nor the CLI sets a reporter (it then uses itsminimalreporter,silent: 'passed-only'). To find a missing fixture, runpnpm exec vitest run --project storybook --silent=false(storybookbeing your storybook project's name), with a story file<file>appended once you know which one makes the request; thetest:storyscript fromsvelte-5:storybook-vitestalways passes--silent - MSW's Node network never sees server-side
event.fetchcalls to your own+server.jsroutes. With SSR on (the default) the browser worker does not mock them on first load either, and fetches from a serverloadnever reach it (sveltekit)
A story renders blank or empty (check in this order)
| Symptom | Cause | Fix |
|---|---|---|
every story fails, Failed to fetch, or [MSW] Failed to register |
worker script not served: suspect it before the handlers | open /mockServiceWorker.js in the browser, or under Vitest look for GET /mockServiceWorker.js with a 404 or 500 in the test output; an error status or a MIME-type error means it is not served (browser integration), so serve it (worker script options above) |
console warns about an unhandled request (in a Vitest run, hidden by --silent, and for passing stories by the minimal reporter an AI agent's run uses when no reporter is set) |
no handler for that URL; under storybook dev, Storybook's own requests can warn too, with the addon's default setup or yours (storybook) |
in a Vitest run, rerun with --silent=false (command in the --silent trap above) to see the warnings; add a handler to the set, or filter Storybook's requests in the callback; a relative path only matches the page origin, so a backend on another origin needs */ or a full URL |
request answered with the wrong body: an empty 200, or a 404 with [mocks] no fixture naming a route you mocked |
a catch-all ordered first, or a string path that ignores the query | catch-alls last; a predicate for the query (handlers) |
only stories with their own parameters.msw lose shared data |
project-level parameters.msw replaced by the story's own |
baseline in setupWorker(...) |
| handlers built by your own loader apply only sometimes, or not at all | loader writes after an await (races mswLoader), or sits on the component or story level (runs too late) |
order and timing in handlers |
E2E
E2E is a separate surface: @msw/playwright binds MSW to Playwright fixtures (use a release whose msw peer range names msw 3). For a one-off override in a Playwright test without MSW, Playwright's own page.route is enough.
Verify
One run of a browser or MSW suite can pass by chance, so clear the caches and take three runs before calling it green (svelte-5:vitest flake hygiene). Only a test project that mounts a component or opens a story exercises the mocks, whatever its script is named.
Reference files and related skills
- migrate-2-to-3: read when upgrading a project from msw 2, together with msw-3: the checklist, what the codemod misses in
.sveltefiles, the Vitest setup file,msw-storybook-addon2 to 3, the old@msw/sveltekitscaffold - msw-3: read for msw 3 specifics: requirements, API facts that break older snippets, one worker per page, the
onUnhandledFramecallback,instanceofandoptimizeDeps, the worker script, navigations and iframes, companion package support, mocks that stop after about five minutes on a long-lived page (msw#2635, msw#2801) - handlers: read when writing or debugging handlers: per-surface sets, the catch-all failure signature, the unmocked-request guard for Vitest browser, query matching, import-safety, stateful factories, generated handlers and loaders
- storybook: read when wiring MSW into Storybook with Svelte CSF: the
preview.tsloader, where the baseline lives, per-story overrides, choosing betweenmswLoader()andcreatePreviewAnnotations(the deprecation path, when to keep the addon out ofmain.tsaddons), the addon codemod's blind spot for.stories.svelte - sveltekit: read on SvelteKit: the
@msw/sveltekitadd-on, what it generates and how to adjust it, HMR in dev, the production build check, the SSR interception blind spot, Storybook and Vitest on SvelteKit svelte-5:storybook-vitest: ownsoptimizeDepsand the Vitest storybook projectsvelte-5:storybook: story authoring, Svelte 4 store wrappers, Playwright verificationsvelte-5:testing-svelte: what to mock at the module seam insteadsvelte-5:vitest: browser projects, flake hygiene, test tagsfrontend:validate: what counts as verified before declaring done (in thefrontendplugin)