PACKAGE · API

msw

Seamless REST/GraphQL API mocking library for browser and Node.js.

WEEKLY DOWNLOADS 8.9M
STARS 18.0K
FORKS 621
OPEN ISSUES 44
GZIP SIZE 122.7 kB
UNPACKED SIZE 5.6 MB
LAST UPDATED 1mo ago
DOWNLOAD TRENDS

msw downloads — last 12 months

Download trends for msw1 download series from Jun 2025 to May 2026. Use left and right arrow keys to inspect monthly values.017.6M35.2M52.8M70.5MJun 2025SepDecMarMay 2026
msw
ABOUT MSW

Mock Service Worker (msw) addresses the critical need for reliable API mocking during frontend and backend development. It provides a way to intercept network requests and return mock responses, preventing developers from depending on actual backend services or unreliable test environments. This is crucial for enabling parallel development between frontend and backend teams, improving test stability, and allowing development to proceed even when the real API is unavailable or under active development.

msw is built around a philosophy of "request interception" rather than "mocking HTTP clients." Its primary audience includes frontend and backend developers working with REST or GraphQL APIs, aiming to create seamless development and testing workflows. The library strives for an "out-of-the-box" developer experience, minimizing configuration overhead and focusing on declarative request handling.

The core API revolves around defining request handlers that match specific HTTP methods, URLs, and optionally request bodies. You can use `rest.get` or `graphql.query` to define these handlers. During runtime, msw uses Service Workers in the browser or a request interception mechanism in Node.js to intercept outgoing requests and serve the defined mock responses. This approach ensures that your application code doesn't need to be aware that it's interacting with mocks.

msw integrates smoothly into various development workflows and frameworks. For browser environments, it leverages the browser's native Service Worker API, making it compatible with single-page applications built with React, Vue, Angular, and others, as well as static sites. In Node.js, it integrates with common testing frameworks like Jest and Vitest, and can also be used for server-side mocking during development or testing of Node.js applications.

The library is designed to be efficient, with a minimal bundle size (122.7 kB gzip) that should not significantly impact application performance in production if accidentally included. Its mature architecture, reflected in 17.9K GitHub stars and an active development cycle, supports both REST and GraphQL. The 5.6 MB unpacked size suggests a comprehensive feature set, but developers should be mindful of this when considering its inclusion in production builds.

While msw is powerful, it's important to note its primary role is API mocking. It does not provide tools for state management or for directly manipulating client-side browser behavior beyond network request interception. For scenarios requiring deep browser simulation or complex state manipulation outside of network interactions, additional tools or different approaches might be necessary. Developers should ensure msw is excluded from production bundles to avoid unnecessary overhead.

WHEN TO USE
  • When developing frontend applications that depend on a REST or GraphQL API, allowing you to mock API responses using `rest.get`, `rest.post`, or `graphql.query`.
  • When building a suite of end-to-end tests (e.g., with Cypress or Playwright) that require stable and predictable API responses, irrespective of backend availability.
  • For parallel development between frontend and backend teams, enabling frontend developers to work against mocked API endpoints defined with `http.get` or `http.post` before the backend is fully implemented.
  • When you need to simulate specific network error conditions, such as 404s, 500s, or slow responses, by configuring mock handlers to return these statuses or delays.
  • For testing your application's behavior with different API data structures or edge cases by easily switching between mock response definitions.
  • When working in Node.js environments for testing API integrations or local development servers, utilizing the Node.js request interception capabilities.
WHEN NOT TO USE
  • If your project exclusively requires mocking simple in-memory state or component props — consider using component-level mocking strategies or dedicated state management libraries in such cases.
  • When you need to mock browser APIs beyond network requests, such as localStorage, navigator, or DOM manipulation — specialized browser mocking tools would be more appropriate.
  • For performance-critical browser applications where even a 122.7 kB gzip bundle size is unacceptable in production — ensure msw is conditionally included or excluded for production builds.
  • If your primary need is to mock outgoing HTTP requests made by a backend service to other external services during integration testing of that backend — you might look for a solution focused on server-side HTTP client mocking.
  • When the complexity of your API interactions is very low, and manually creating mock data within your tests provides a simpler and more direct approach without the overhead of setting up a mocking service worker.

CORRECTIONS

Spot wrong data here?

A short note helps us fix it.

Anonymous · No account · No email back

COMPARISONS 5
msw vs @trpc/server ★ 40.3K · 1.9M/wk msw vs graphql ★ 20.3K · 19.3M/wk msw vs googleapis ★ 12.2K · 4.3M/wk msw vs @google/genai ★ 1.6K · 6.9M/wk msw vs openapi-typescript ★ 8.2K · 2.1M/wk