Mock Service Worker (MSW) is an API mocking library for JavaScript that intercepts network requests at the service worker level in the browser and at the request handler level in Node.js. It solves the problem of testing and developing frontend applications that depend on backend APIs by providing a seamless way to mock API responses without modifying application code or setting up proxy servers. MSW operates by intercepting actual network requests rather than stubbing the fetch or XMLHttpRequest implementations, which means your application code behaves identically whether it talks to a real or mocked API.
MSW provides a request handler API for defining mock responses using a familiar Express-like syntax, support for REST and GraphQL APIs, network-level interception that preserves the application's actual networking behavior, and the ability to share the same mock definitions between browser tests, Node.js integration tests, and development environments. It supports response delays, error simulation, and conditional responses for testing edge cases. MSW also offers lifecycle events for observing and modifying requests and responses, and type-safe request handlers when used with TypeScript.
MSW is essential for frontend developers who need reliable API mocking during development and testing without coupling their mocks to specific testing frameworks. It integrates with Jest, Vitest, Cypress, Playwright, Storybook, and React Testing Library, providing a consistent mocking layer across all testing and development contexts. MSW is particularly valuable for teams building applications against APIs that are still in development, frequently changing, or expensive to call, enabling parallel frontend and backend development without blocking dependencies.