Stubsmith vs MSW: hand-written or recorded mocks

MSW serves mock responses you write, in the browser and in Node. Stubsmith records real outbound HTTP calls, masks them in your process and replays them.

MSW and Stubsmith answer different questions. With MSW you write request handlers that describe how an API should respond, and MSW serves them in the browser through a Service Worker or in Node inside your process [1][2]. With Stubsmith you do not write the responses: the SDK records the real outbound HTTP calls your Python or Node code makes, masks every body value in your process before the capture is sent, and your tests replay what the API actually returned.

If the API does not exist yet, if you need mocks in the browser, or if you want to design a specific error on demand, MSW is the tool for that and Stubsmith is not. If your hand-written mocks keep drifting from what a third-party API really sends, recording is the alternative, and that is what Stubsmith does.

At a glance

MSW Stubsmith
Where responses come from Handlers you write in code [3]; a separate package, @msw/source (version 0.7.0), generates handlers from a HAR file, replaying repeated responses in recorded order, or from an OpenAPI document [4][15][16] Recorded from real calls your code makes
Runs in Browser (Service Worker) and Node (in-process); React Native through a separate package [2][5] Python and Node processes
APIs HTTP, GraphQL, server-sent events, WebSocket [6] HTTP
A request with no match Default "warn" prints a warning and performs the real request; onUnhandledFrame: "error" halts it instead [7] Raises StubNotFound; never reaches the network
Real data in mocks Only what you put in a handler, or what is in a HAR file you load [4] Body values masked in your process; a body value is sent only for a field you approved
Where mocks live In your codebase, for example a handlers.js module [3] A bundle file you commit, pulled from the reviewed set on Stubsmith
License and price MIT, free [8] SDKs MIT; hosted service with a free plan

How each one works in a test

MSW starts before your tests, resets handlers between them, and stops after them; a test can prepend handlers with server.use() to override a response [9]. You write each response yourself, including the error cases.

Stubsmith records first and replays later. In your app or a test run against a real environment:

import stubsmith from 'stubsmith';
stubsmith.install(); // key read from STUBSMITH_API_KEY

After you approve the new request shapes in Review, pull the bundle once and commit it:

npx stubsmith pull --out .stubsmith/bundle.json

Then replay in Vitest or Jest, with no network and no key:

import { beforeEach, afterEach } from 'vitest';
import { replay } from 'stubsmith';

let ctx;
beforeEach(() => { ctx = replay(); });
afterEach(() => { ctx.restore(); });

replayAll() runs a block once per recorded status, so a test meets the 200, the 402 and the 500 the API really returned. After stubsmith pull --samples all --endpoint ... it runs once per stored sample, 3 to 25 per response depending on plan.

Recorded traffic and personal data

MSW’s handlers contain whatever you write, so no real data is involved unless you bring it in. @msw/source can turn a HAR file recorded in browser DevTools into handlers [10]. The HAR file stays in your project; MSW’s documentation, checked on the date below, does not cover removing personal data from it, though fromTraffic accepts a map function that can rewrite or skip entries [15].

Stubsmith records production-shaped traffic, so it masks during capture. A body value without a keep rule becomes "<masked>", 0 or false before the capture leaves your process, and by default no field has a keep rule. Two kinds of value are sent as written: a path segment the SDK does not recognise as an ID, and the values of six transport headers a stub needs to replay. Names are sent as they are: JSON keys, form field names, query-parameter names and header names. So are the method, domain, status and duration of each call. The Security page lists all of it.

Choose MSW when

  • You are mocking an API that does not exist yet, or you design the API as you go [11].
  • You need mocks in the browser, in Storybook or in a live demo, not only in tests [1].
  • You want to produce a specific case on demand: a network error, a timeout, a 503, a delay [12][13][14].
  • You mock GraphQL, server-sent events or WebSocket [6].

Choose Stubsmith when

  • You call third-party HTTP APIs from Python or Node, and you want tests to use what those APIs really return rather than what someone wrote down.
  • You want the rare responses your traffic produced (the 402, the 429, the odd 500) without writing each one.
  • Real body values must stay in your process unless you approve a field.

MSW and Stubsmith can sit in the same project: MSW for browser code and designed scenarios, Stubsmith replay for the server-side calls to third-party APIs.

What Stubsmith does not do

  • Mock an API you have never called. Stubs come from recorded traffic.
  • Run in the browser or in React Native.
  • Simulate network errors or delays, or change a response: replay serves what was recorded.
  • Mock GraphQL subscriptions, server-sent events or WebSocket.
  • Capture Node calls made through undici’s request(), http2 or WebSocket.

Sources

Facts about MSW are from MSW's own documentation and repository (version 3.0.7), last checked 2026-11-19. Corrections are welcome at legal@stubsmith.dev.

  1. MSW documentation
  2. setupServer
  3. Structuring handlers
  4. Source
  5. React Native
  6. Quick start
  7. server.listen() options
  8. License
  9. Node integration
  10. Recording a HAR
  11. Repository README
  12. Network errors
  13. Response timing
  14. Error responses
  15. Source, HAR integration
  16. @msw/source on npm

Integration tests from real API behavior

Capture once, replay in CI, with values masked before they leave your process.