Quick start
Add MSW to your project in under five minutes.
Mock Service Worker (MSW) is an API mocking library for browser and Node.js. It helps you intercept, observe, and affect the network of your application.
What sets MSW apart is that it heavily advocates for a standalone API mocking layer, creating a single source of truth for your API mocks across your entire stack. This results in a more resilient setup and, paired with other library features, creates a truly seamless API mocking experience.
MSW is designed to be fully environment-, framework- and tool-agnostic. You can use it in any browser or Node.js process without additional configurations, adapters, or plugins. It works with all request clients, be it a native fetch or third-party libraries like Axios, React Query, or Apollo.
MSW uses the Service Worker API to intercept actual production requests on the network level. Instead of patching fetch and meddling with your application's integrity, MSW bets on the platform, utilizing the standard browser API to implement a revolutionary request interception logic.
Even in Node.js, where there are no standard means to intercept requests, MSW uses class extension instead of module patching to ensure your tests run in the environment as close to production as possible.
By treating API mocking as a standalone layer, MSW can integrate throughout your entire stack, allowing you to reuse and customize network behavior on demand. Imagine using the same API mocks during development, integration and end-to-end testing, and then in your Storybook or during a live demo. Well, with MSW, you can.
If you have never tried MSW before or are unsure where to start, look no further than the Quick start tutorial:
Quick start
Add MSW to your project in under five minutes.
This documentation has everything you need to mock RESTful, GraphQL, and WebSocket APIs.
If you prefer learning in a more interactive, build-along format, consider one of our official Egghead courses below. These courses require a paid subscription and help us keep the project alive.