Mock Service Worker lets you define HTTP mocks once and reuse them in browsers and Node.js. Applications keep calling real APIs while responses are substituted at the network layer.
Overview
Mock Service Worker (MSW) is a JavaScript library for API mocking in browsers and Node.js. You describe request handlers once and wire them through either a Service Worker in the browser or a Node interception layer, so application code still performs real fetches while selected traffic receives mocked responses.
In the browser, MSW uses the Service Worker API to intercept outgoing requests after they leave your application. Handlers can target REST, GraphQL, server-sent events, and WebSocket traffic. Mocks can appear in developer network tools because the request path stays intact rather than replacing client HTTP code with stubs.
On Node.js, MSW provides setupServer with the same handlers, using a low-level interception approach because Service Workers are unavailable there. The same definitions can support local development, debugging, unit tests, integration tests, and browser end-to-end tooling such as Vitest and Playwright fixtures.
The library emphasizes client-agnostic handlers built around familiar Fetch-style APIs, so mocks are not tied to a single HTTP client. Optional companion projects mentioned in upstream materials include generating handlers from OpenAPI or HAR, data tooling, and serving handlers as HTTP middleware in frameworks like Express, Hono, and Fastify.
Key features
- Browser Service Worker interception
- Node.js setupServer API
- Shared handlers across environments
- REST, GraphQL, SSE, and WebSocket mocks
- Playwright network fixture support
- Storybook addon integration
- Express server boundary scoping
- Fetch-oriented handler syntax
Use cases
- Frontend development without live backends
- Automated tests with stable network behavior
- Prototyping API contracts before they exist
- Debugging UI against error and edge responses
- End-to-end tests with realistic request paths