Mock Service Worker (MSW) is a powerful and seamless library for intercepting and mocking REST and GraphQL API requests directly at the network level, in both browser and Node.js environments. Unlike traditional mocking libraries that patch client-side request utilities, MSW leverages the Service Worker API in browsers and a custom `http` interception module in Node.js. This approach allows applications to run against mock data without any code changes, making it ideal for consistent mocking across development, unit, integration, and end-to-end testing, as well as debugging. The current stable version is 2.13.4, with frequent minor and patch releases, often several times a month, indicating active development. Its key differentiator is providing deviation-free mocking by intercepting actual network requests, ensuring a high fidelity simulation of real API interactions.
npm install mswVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up a browser-based Mock Service Worker to intercept and respond to HTTP GET and POST requests, followed by actual `fetch` calls that are intercepted by the mock server.
Migrate your project to use ECMAScript Modules (ESM) syntax (`import ... from '...'`) and ensure your `package.json` has `"type": "module"` or uses `.mjs` file extensions. Update your bundler/build configuration if necessary.
Replace `rest.get`, `rest.post`, etc., with `http.get`, `http.post`, and similar for other HTTP methods. Review the official migration guide for other API changes in v2, particularly for GraphQL handlers.
Update your import statements: `import { setupWorker } from 'msw'` becomes `import { setupWorker } from 'msw/browser'` for browser usage, and `import { setupServer } from 'msw'` becomes `import { setupServer } from 'msw/node'` for Node.js usage.Run `npx msw init <PUBLIC_DIR_PATH>` to automatically generate/update the worker script. Verify that the `mockServiceWorker.js` file is accessible at the root of your application (e.g., `http://localhost:3000/mockServiceWorker.js`). If using a custom path, ensure `setupWorker` is configured with `serviceWorker.url`.
Wrap your `setupServer` calls in test framework lifecycle hooks: `beforeAll(() => server.listen());` `afterEach(() => server.resetHandlers());` `afterAll(() => server.close());` Refer to MSW documentation for specific test runner integrations.
Ensure you are using `import { setupWorker } from 'msw/browser'` and that your project is configured for ESM. For Node.js, use `import { setupServer } from 'msw/node'`.Verify that `mockServiceWorker.js` is present in your public directory and accessible. Run `npx msw init <PUBLIC_DIR_PATH>` to re-initialize it. Check your web server configuration to ensure it serves static files correctly from the root path. If using a custom path, specify it in `setupWorker({ serviceWorker: { url: '/custom/path/mockServiceWorker.js' } })`.Ensure `setupServer` is correctly imported as `import { setupServer } from 'msw/node'` and that the `server` variable is assigned the result of `setupServer(...)`.Replace `rest.get`, `rest.post`, `rest.put`, `rest.patch`, `rest.delete` with their `http` equivalents (e.g., `http.get`, `http.post`).