BASE

Mock Service Worker

Mock Service Worker

Test automation · Free

Intercepts network requests in browsers and Node.js, allowing application checks with realistic API mocks without a separate server.

Also known as: mswjs/msw; mock-service-worker; msw-api-mocking

Resource illustration: Mock Service Worker

Open resource ↗

What you can use for free

Code and local use under MIT.

Paid features and costs

Compute, your own hosting, and external services are paid for separately if selected.

Access and limitations

Code and local use under MIT. Compute, your own hosting, and external services are paid for separately if selected.

Use cases

  • Check an interface with reproducible API responses
  • Connect interception to a browser or Node.js test environment

Example workflow

A possible workflow; this is not a report of hands-on testing.

Check an interface with reproducible API responses.

Before you start

  • One important workflow and its expected result.
  • A test environment with controlled data.

Steps

  1. Define request handlers for the required endpoints and response states.
  2. Connect interception to a browser or Node.js test environment.
  3. Run the check locally and save the report in CI.

Check your result

  • The check detects a deliberately broken workflow.
  • The result can be reproduced without depending on a personal session.

Choosing Mock Service Worker

Check an interface with reproducible API responses

Choose this when

  • Check an interface with reproducible API responses
  • Connect interception to a browser or Node.js test environment

Requirements

  • Choose browser/node setup and configure request handlers for the APIs in use.

Things to consider

  • Costs and limits of free use: Compute, your own hosting, and external services are paid for separately if selected.
  • Terms of use: MIT: code; dependencies have separate licenses.
  • Browser and Node interception use different mechanisms; the browser route requires a Service Worker, while Node does not.

Frequently asked questions

What can you use for free in Mock Service Worker?

Code and local use under MIT.

What additional costs might arise with Mock Service Worker?

Compute, your own hosting, and external services are paid for separately if selected.

What terms of use matter for Mock Service Worker?

MIT: code; dependencies have separate licenses.

How do you get started with Mock Service Worker?

Define request handlers for the required endpoints and response states. Connect interception to a browser or Node.js test environment. Run the check locally and save the report in CI.

Does MSW use the same mechanism in browsers and Node.js?

In a browser, MSW uses a Service Worker to intercept requests at the network level; handlers execute on the client rather than inside the worker. Node.js has no Service Worker and uses low-level interception. Shared request handlers can be reused for development, unit/integration tests, and E2E, adding browser setupWorker or node setupServer according to the environment.

Related tasks and approaches

License

MIT: code; dependencies have separate licenses.

MIT — permissive license

You can use, modify, distribute, and sell the code, including in a closed-source commercial product. When sharing copies or substantial portions of the code, retain the license text and copyright notice. You do not need to open-source your entire project; the author provides no warranties.

License terms

Technologies and topics

  • Mock Service Worker
  • test
  • Development

Sources

Other resources