DevXTools

MSW vs Nock vs Polly.js: Choosing a JavaScript HTTP Mocking Library

Handlers, interceptors or recordings: pick by runtime and approach

TL;DR

Pick MSW if your mocks need to run in the browser as well as Node, or you want one set of handlers for tests, the dev server and Storybook. Pick Nock if you only test Node code and want strict per-test expectations plus built-in recording (nock.back). Polly.js still works for record/replay in Node and the browser, but its core hasn’t had a release since July 2023, so for new projects look at maintained Polly.js alternatives first.

Only deciding between the first two? Read MSW vs Nock head-to-head, with code.

Three approaches to HTTP mocking

  • Handlers (MSW): you describe the API with request handlers, and MSW answers matching requests at the network level, through a Service Worker in the browser and request interception in Node.
  • Interceptors with expectations (Nock): each test declares which outbound requests must happen and what they return. Nock is Node-only.
  • Recordings (Polly.js): you run the test against the real API once, Polly saves the traffic, and later runs replay it. Adapters cover fetch, XHR, Node’s HTTP module and Puppeteer.

Quick comparison

MSWNockPolly.js
ApproachRequest handlersPer-test expectationsRecord and replay
BrowserYesNoYes
Node.jsYesYesYes
Built-in record/replayNoYes (nock.back)Yes (core feature)
Latest release (checked 2026-10-19)3.0.3 (2026-10-10)15.0.2 (2026-10-16)core 6.0.6 (2023-07-20); adapter-fetch 6.0.7 (2025-05-31)

For the line-by-line MSW and Nock code (Jest and Vitest setup, POST matching, errors, delays, assertions), see MSW vs Nock head-to-head, with code.

Which library for which runtime?

You’re testing…Start withNotes
Node backend, SDK or CLINock or MSWNock for strict expectations; MSW if the same handlers are reused elsewhere.
Browser app (unit tests, dev server, Storybook)MSWNock can’t run in the browser.
React NativeSee our React Native mocking guideMSW 3 removed msw/native, and its replacement isn’t on npm yet.
Playwright E2EMSW (@msw/playwright) or Playwright’s own route / routeFromHARRecording a HAR and replaying it is built into Playwright.
Replaying recorded real trafficNock’s nock.back (Node) or a maintained recorderSee maintained Polly.js alternatives.

MSW (Mock Service Worker)

Strengths

  • The same handlers work in Node tests, the browser, Storybook and Playwright.
  • Intercepts at the network level, so your app code and HTTP client stay unchanged.
  • Supports REST, GraphQL and WebSocket.

Limits

  • No built-in recorder.
  • Browser use needs the Service Worker script set up.
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'

const server = setupServer(
  http.get('https://api.example.com/api/user', () => {
    return HttpResponse.json({ id: 1, name: 'John' })
  })
)

beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

How it stacks up against Nock in detail: MSW vs Nock head-to-head, with code.

Nock

Strengths

  • A small API with strict, one-shot expectations that fail when an expected request isn’t made.
  • Built-in record and replay with nock.back.
  • Supports native fetch since v14.

Limits

  • Node.js only.
  • Don’t combine it with MSW in the same test file, since both intercept the same layer.
import nock from 'nock'

nock('https://api.example.com')
  .get('/user/1')
  .reply(200, { id: 1, name: 'John' })

// Matching requests in your test are now intercepted

Polly.js in 2026: when it’s still fine

Maintenance status (checked October 2026). The latest @pollyjs/core is 6.0.6, released 2023-07-20. The only release since then is @pollyjs/adapter-fetch 6.0.7 on 2025-05-31. The GitHub repository (Netflix/pollyjs) isn’t archived. Sources: npm registry, GitHub.

Polly.js records real HTTP traffic and replays it, with adapters for fetch, XHR and Node’s HTTP module, and persisters for the file system, local storage and a REST server. It is still a reasonable choice if:

  • An existing suite already uses it and the recordings are working.
  • You’re on the adapters it supports and don’t need fixes upstream.

For new projects, a library without a core release in more than three years is a risk. Compare maintained Polly.js alternatives before you adopt it.

import { Polly } from '@pollyjs/core'
import NodeHttpAdapter from '@pollyjs/adapter-node-http'
import FSPersister from '@pollyjs/persister-fs'

Polly.register(NodeHttpAdapter)
Polly.register(FSPersister)

const polly = new Polly('My Test', {
  adapters: ['node-http'],
  persister: 'fs'
})

// Polly records and replays automatically

Which should you choose?

MSW · Nock · Polly.js · MSW vs Nock head-to-head, with code · Polly.js alternatives · record/replay at the service layer, not only in a JS test · All comparisons

Sources (checked 2026-10-19)

Find tools for your stack

Answer a few questions to find tools that match your needs.