API mocking library for JavaScript that intercepts requests at the network level using Service Workers.
Mock Service Worker (MSW) is an API mocking library for JavaScript that intercepts requests at the network level, enabling seamless mocking for development, testing, and debugging. It works in both browser and Node.js environments, allowing developers to reuse the same mock definitions across different tools and stages of the development lifecycle. It uses a Service Worker in browsers and a low-level interception algorithm in Node.js to leave application code intact.
JavaScript developers building web applications who need to mock API calls for local development, unit testing, integration testing, and end-to-end testing without modifying their application's request logic. It is particularly useful for teams aiming to maintain a single source of truth for network behavior across environments.
Developers choose MSW because it intercepts requests externally at the network level, avoiding the need to stub internal libraries like fetch or axios, which keeps application code production-like and unaware of mocking. Its environment-agnostic design allows the same mock definitions to be reused across browsers, Node.js, and various testing frameworks, reducing configuration overhead and increasing confidence in tests.
Industry standard API mocking for JavaScript.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Intercepts requests after they leave the application, ensuring all code runs and providing high confidence in tests, as emphasized in the 'How does it work?' section comparing it to other tools that stub internally.
Allows the same mock definitions to be used in browsers, Node.js, and across testing frameworks, reducing duplication and maintaining consistency, highlighted in the 'Environment Agnostic' feature.
Uses Express-like routing with parameters and wildcards, making it easy to define complex request handlers, as described in the 'Familiar & Powerful' feature of the README.
Leaves application code untouched by mocking externally, avoiding stubbing of internal libraries like fetch or axios, which keeps behavior close to production, as noted in the 'Deviation-free' philosophy.
In browsers, MSW requires Service Worker registration, which can fail in environments with strict security policies or require additional configuration, adding setup complexity and potential compatibility issues.
Starting the Service Worker or server involves asynchronous operations and configuration steps that might be more involved than simpler mocking solutions, especially for quick prototyping or simple tests.
While basic HTTP mocking is robust, advanced features like WebSocket mocking require additional setup or paid courses, as indicated in the partnered learning materials, suggesting core support may be incomplete.