Skip to content
Latchkey

MSW vs nock: Which API Mocking Tool?

MSW intercepts requests at the network layer for browser and Node alike; nock intercepts Node HTTP at the module level for server tests.

MSW (Mock Service Worker) defines request handlers once and intercepts at the network boundary, working in the browser (via a Service Worker) and in Node, so the same mocks serve unit tests, component tests, and even local dev. nock patches Node's HTTP layer to intercept and assert outgoing requests, focused purely on Node/server testing. MSW wins on cross-environment reuse and fidelity; nock wins as a focused, mature Node HTTP mocker.

MSWnock
EnvironmentsBrowser + NodeNode only
InterceptionNetwork layerNode HTTP module
ReuseTests + dev + storybookServer tests
FidelityHigh (real network path)Module-level
Best forUniversal frontend/back mocksNode HTTP test mocking

Use case and scope

MSW suits teams wanting one set of handlers across browser tests, component tests, Storybook, and local development. nock suits Node/server test suites that only need to intercept and assert outgoing HTTP at the module level. MSW's cross-environment reach is its standout; nock's focus keeps it simple for server-only mocking.

Testing and CI

Both run headless in CI; MSW's Node integration covers component and unit tests, nock covers server calls. Either runs on managed runners, where faster runners shorten mocked integration suites.

The verdict

Want one mocking layer across browser, Node, tests, and dev: MSW. Want a focused Node HTTP interceptor for server tests: nock. MSW is the versatile universal choice; nock is the lean Node-only specialist.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card