Skip to content

MSW vs Nock

Which should you use? A side-by-side look at what each one does, who it suits, what it costs, how hard it is to set up, and where each falls short.

MSW

Open source · MIT

A JavaScript library that intercepts API requests so your app can use mock responses.

Nock

Open source · MIT

Mocks HTTP requests in Node.js so tests can run without calling real servers.

The short answer

Choose MSW if you need…

  • Frontend developers who need to work before a backend API is ready
  • Teams that want the same mock definitions for unit, integration, and end-to-end tests
  • Developers who want mocked responses to show up in the browser DevTools Network tab
  • Node.js developers who want to mock outgoing requests from an Express server during development

Choose Nock if you need…

  • Developers writing automated tests for Node.js code that makes HTTP calls
  • Teams who want tests isolated from external services like third-party APIs
  • Projects needing to simulate error responses or delays in a controlled way

Side-by-side

MSWNock
TypeLibraryLibrary
PriceFree and open sourceFree and open source
LicenceMITMIT
GitHub stars18.3k13.1k
Setup difficultyIntermediateIntermediate
How you get itInstall with a package managerInstall with a package manager
Main languageTypeScriptJavaScript
Last code update20 days ago7 days ago

What each one is

MSW

Mock Service Worker (MSW) lets developers fake the responses of network requests made by their app. It works in the browser by using the Service Worker API, and in Node.js by intercepting requests at a low level, so the app's own code can run unchanged. Developers use it for local development, debugging, and automated tests.

Nock

Nock is a JavaScript library for Node.js that fakes HTTP responses during testing. It works by overriding Node's built-in HTTP request function, so code that calls external APIs can be tested without contacting those APIs. You describe which requests to expect and what replies to return.

Best for

MSW

  • Frontend developers who need to work before a backend API is ready
  • Teams that want the same mock definitions for unit, integration, and end-to-end tests
  • Developers who want mocked responses to show up in the browser DevTools Network tab
  • Node.js developers who want to mock outgoing requests from an Express server during development

Nock

  • Developers writing automated tests for Node.js code that makes HTTP calls
  • Teams who want tests isolated from external services like third-party APIs
  • Projects needing to simulate error responses or delays in a controlled way

Strengths

MSW

  • Intercepts requests at the network level, so the app code does not need to be changed for mocking
  • Uses Express-like routing syntax with parameters, wildcards, and regular expressions
  • The same request handlers can be used in the browser and in Node.js

Nock

  • Widely used, with about 13,000 GitHub stars and an MIT license
  • Flexible matching of hostnames, paths, bodies, query strings and headers
  • Supports persisting or sequencing interceptors and checking whether mocks were used

Not ideal for

MSW

  • People who only need static fixture files and no request interception (the README does not describe such a use case, so a simpler tool may be enough)
  • Non-developers, since the library is configured with JavaScript or TypeScript code

Nock

  • Non-developers, since it is a code library that requires writing tests in JavaScript
  • Projects not running on Node.js, because the README describes it specifically for Node.js
  • Anyone needing to test against a real live server, since its purpose is to replace that server

Trade-offs

MSW

  • Requires understanding of Service Workers for browser setup, which adds some learning effort
  • Documentation and quick start are hosted on an external site, so the README alone gives limited setup detail
  • The README describes it as maintained in spare time, so project capacity may vary

Nock

  • Interceptors are removed after use by default, so each expected request needs its own setup unless you use persist()
  • The README lists a compatibility table of which Node versions each old release supported, which means version choice matters

Getting started

MSW

Clone the repository

git clone https://github.com/mswjs/msw
Step-by-step guide: how to use MSW

Nock

Install as a dev dependency

$ npm install --save-dev nock
Step-by-step guide: how to use Nock

Frequently asked questions

What is the difference between MSW and Nock?

MSW: A JavaScript library that intercepts API requests so your app can use mock responses. Nock: Mocks HTTP requests in Node.js so tests can run without calling real servers. MSW suits frontend developers who need to work before a backend api is ready, while Nock suits developers writing automated tests for node.js code that makes http calls.

Are MSW and Nock free?

MSW: Free and open source. Nock: Free and open source.

Is MSW or Nock more popular?

MSW has more GitHub stars (18.3k) than Nock (13.1k). Popularity isn't the same as fit — check which one matches your use case above.

What are other alternatives to MSW and Nock?

Other options people compare them with include Mockito, Workbox, testify.

Keep comparing

Built from our catalogue entries for both products: open-source details come from each project's README (AI-assisted, refreshed weekly) and GitHub data; AI-tool details are editorial. Spotted something wrong? Let us know from either product's page.