substitutefor

software · Q989

Mock Service Worker

MIT-licensed API mocking library that intercepts application requests in browser and Node.js environments using reusable handlers.

Suggest a correction

Overview

Mock Service Worker, commonly called MSW, is an open-source library for controlling application network behavior during development and testing. Developers define request handlers that can be reused in browser and Node.js integrations. This can exercise a frontend's loading, success and error paths without waiting for a real backend, but the mocks are an authored model of that backend rather than proof of its behavior.[2][3]

A shared mock layer

MSW separates API behavior from the request client used by application code. Handlers can describe controlled responses and be reused across development, integration tests and demonstrations. The documented approach keeps those mocks outside individual fetch replacements, making the same behavior available to multiple tools. As an assessment, fixtures should still represent meaningful backend contracts: a frontend working against a fabricated response does not establish that the real service returns it.[2]

Browser and Node integration

Handlers alone do not activate interception. The browser and Node integrations use different setup entry points, and test setup must enable and later close interception. The quick start resets runtime handler changes after each test. In Node, setupServer is an interception layer inside the process, despite its name; another program cannot simply connect to it as a listening HTTP server. Shared handlers must also avoid unprovided browser-only globals when used in Node.[3][4]

Unmatched requests may reach real services

The default HTTP model is passthrough unless handlers supply another behavior. Handler order is significant, and an override can take precedence over an earlier fixture. The defaults guide describes WebSocket interception differently as mock-first. Current listen documentation uses onUnhandledFrame, with warning-and-continuing as the default and an error strategy that halts unhandled frames. Consequently, installing MSW alone should not be treated as a guarantee that tests cannot contact real services.[5][6]

Interception boundaries

Browser interception inherits Service Worker limitations. The limitations guide identifies missing XMLHttpRequest progress events and a Firefox XMLHttpRequest interception caveat. It also says direct Node net connections are outside the intercepted request path. These are reasons to verify the actual client and runtime, rather than assume a mock covers all traffic merely because a handler matches the intended URL. This article does not extrapolate the socket limitation to every use of native fetch.[7]

Version matching

The inspected repository manifest identifies version 3.0.2 and separate protocol exports, while the current API documentation uses network-frame terminology. Existing projects should match examples to their installed major version rather than splice current setup options into an older tutorial. This qualification is especially useful for generated test scaffolding, where a plausible import or option name can belong to another release.[6][8]

Licensing

MSW's covered source is MIT-licensed, including its notice-preservation conditions. The documentation separately offers paid educational material, but the source license does not require purchasing that material to use the library. Third-party services contacted by passthrough traffic keep their own access terms and costs.[1][2][5]

Substitution considerations

MSW can replace Mockoon for application-owned HTTP fixtures when code-level interception is preferable to running a separate endpoint. Mockoon's desktop and CLI server model remains different: it can serve clients outside one instrumented process. Moving between them involves changing fixture format and integration, not simply switching a URL. No installation, browser compatibility test or live API validation was performed.[2][4][9]

Capabilities & limitations

Reusable API handlers

supported

Handler definitions can serve development and tests with browser or Node integration.[2][3]

Controlled API responses for frontend development and automated tests · Browsers with compatible Service Worker support, Node.js · Version, build and integration requirements apply

2 sources · check evidence
https://mswjs.io/docs/

Browser/Node interception and reusable API mocks

vendor · checked 2026-10-07
https://mswjs.io/docs/quick-start

Handlers, setupServer/setupWorker and lifecycle reset

vendor · checked 2026-10-07

Standalone listening mock server

unsupported

setupServer configures interception inside the current process; it does not create a network server.[4]

Controlled API responses for frontend development and automated tests · Browsers with compatible Service Worker support, Node.js · Version, build and integration requirements apply

1 source · check evidence
https://mswjs.io/api/setup-server

Process-local interception; no listening server; browser-specific handler APIs

vendor · checked 2026-10-07

Automatic isolation from real HTTP services

unsupported

Unhandled HTTP traffic can pass through; current listen options default to warning while continuing the frame.[5][6]

Controlled API responses for frontend development and automated tests · Browsers with compatible Service Worker support, Node.js · Version, build and integration requirements apply

2 sources · check evidence
https://mswjs.io/docs/defaults

HTTP passthrough, WebSocket difference and handler order

vendor · checked 2026-10-07
https://mswjs.io/api/setup-server/listen

onUnhandledFrame warn/error/bypass behavior

vendor · checked 2026-10-07

Cross-browser and direct-socket interception

limited

Documented Service Worker/XHR caveats and direct net connection limits prevent blanket coverage.[7]

Controlled API responses for frontend development and automated tests · Browsers with compatible Service Worker support, Node.js · Version, build and integration requirements apply

1 source · check evidence
https://mswjs.io/docs/limitations

Service Worker/XHR progress/Firefox and direct Node socket limitations

vendor · checked 2026-10-07

Can substitute for

These are products or services this entry can replace. Check the task and limitations for each comparison.

Mockoon for Controlled API responses for frontend development and automated tests

limited

Can replace a separate Mockoon endpoint for code-controlled browser/Node application tests by intercepting the application's requests. It does not replace a shared listening server for arbitrary external clients, and fixtures must be rewritten as handlers.[2][4][9]

Browsers with compatible Service Worker support, Node.js · Version, build and integration requirements apply

References

  1. MSW license

    MIT conditions

    vendor · retrieved 2026-10-07
  2. MSW introduction

    Browser/Node interception and reusable API mocks

    vendor · retrieved 2026-10-07
  3. MSW quick start

    Handlers, setupServer/setupWorker and lifecycle reset

    vendor · retrieved 2026-10-07
  4. MSW setupServer

    Process-local interception; no listening server; browser-specific handler APIs

    vendor · retrieved 2026-10-07
  5. MSW default behaviors

    HTTP passthrough, WebSocket difference and handler order

    vendor · retrieved 2026-10-07
  6. MSW listen options

    onUnhandledFrame warn/error/bypass behavior

    vendor · retrieved 2026-10-07
  7. MSW limitations

    Service Worker/XHR progress/Firefox and direct Node socket limitations

    vendor · retrieved 2026-10-07
  8. MSW package manifest

    Inspected version 3.0.2 and protocol-specific exports

    vendor · retrieved 2026-10-07
  9. Mockoon repository

    Desktop-authored servers and headless CLI

    vendor · retrieved 2026-10-07

Discussion & disputes

Ask about unclear claims or explain a concern with supporting sources. If you have a specific fix, suggest a correction.

No discussion yet.

Sign in with an active account to join the discussion.

Revision history

Open a revision to read an earlier version of this article.

RevisionTimestampEdit summary
10372026-10-13 18:03:49/* wbeditentity-create-item:0| */ sf:5939ee74-e70a-4f69-b21e-755ccbaebf53:v1 | Adds MSW with process-local interception versus mock servers, real-network passthrough defaults, v3 API qualifiers and documented browser/socket limits. | attribution: /proposals/5939ee74-e70a-4f69-b21e-755ccbaebf53