sv community add-on: @msw/sveltekit
Important
Svelte maintainers have not reviewed community add-ons for malicious code. Use at your discretion.
You can create a new SvelteKit project with this add-on using the following command:
npx sv create --add @msw/sveltekitOr integrate MSW add-in into an existing project with this one:
npx sv add @msw/sveltekitmswadded as a dev dependency.src/mocks/handlers.tsorsrc/mocks/handlers.jswith shared request handlers.- The
msw()plugin frommsw/viteadded to your Vite config. - Optional browser and server setup in the corresponding
src/hooks.clientandsrc/hooks.serverinithooks. - Types for
virtual:mswreferenced insrc/app.d.ts.
The hooks load your handlers and enable the virtual:msw network during development only. The Vite plugin selects the browser or Node implementation automatically and serves the browser worker, so there is no need to run msw init or copy a worker into static.
The add-on installs the latest stable MSW release. The Vite integration requires MSW 3.0.0 or later. You can override the dependency with the MSW_VERSION environment variable.
Choose where MSW should run during development: browser, node (the SvelteKit server), or both. This is a multiselect option.
Default: browser,node
npx sv add @msw/sveltekit="environments:browser,node"