Mock Service Worker usage example with Svelte.
Both client-side and server-side integrations of MSW are implemented using Hooks.
- Describe the request handlers in
src/mocks/handlers.ts
.
- Generate the worker script (
mockServiceWorker.js
) in the./static
directory using the following command:
npx msw init ./static
- Create
src/mocks/browser.ts
and configure theworker
instance. - In
src/hooks.client.ts
, import and start theworker
fromsrc/mocks/browser.ts
.
The server-side integration allows you to intercept outgoing requests from the load
functions of your Svelte pages.
- Create
src/mocks/node.ts
and configure theserver
instance. - In
src/hooks.server.ts
, import and start theserver
fromsrc/mocks/node.ts
.