mswMock Service Worker

Comparison

Below you can find an unbiased comparison between Mock Service Worker and other open source mocking libraries. Be mindful when choosing a solution, and make sure it suits your application's needs the most.

This page will not reason about what technology is better or worse. All the libraries listed here have a place to be, and grading them is not only subjective, but disrespectful towards the people who really should be using one over another.

Comparison criteria

  • Supported API. List API types that can be mocked.
  • Environments. List of environments where a mock can be run.
  • Implementation. Brief high-level explanation on how a library works.
  • Integration. Steps to perform in order to integrate a library into your project.
  • Definition. How the mocking logic is defined.

Nock

Nock is an HTTP server mocking and expectations library for NodeJS.

CriteriaNockMock Service Worker
Supported APIRESTREST / GraphQL
EnvironmentsNodeNode / Browser
ImplementationIntercepts requests by monkey patching http/https/XMLHttpRequest modules.Node
Intercepts requests by monkey patching http/https/XMLHttpRequest modules.
IntegrationDoes not require any changes to an existing code. Requires additional adapters for request issuing libraries (i.e. axios).Does not require any changes to an existing code. Does not require any additional adapters.
DefinitionResponse mocking through method chaining.Response mocking through functional composition.

Nock is a great tool for combining API mocking and assertions in NodeJS. NodeJS API of Mock Service Worker functions similarly to Nock, but allows you to reuse the same mock definition in browser as well.

JSON Server

JSON Server allows you to create an actual HTTP server based on a JSON file.

CriteriaJSON ServerMock Service Worker
Supported APIRESTREST / GraphQL
EnvironmentsNode / BrowserNode / Browser
ImplementationSpawns an actual HTTP server that you can curl, but also have to start and stop.Browser
Intercepts requests on a network level using Service Worker.

Node
Intercepts requests by monkey patching http/https/XMLHttpRequest modules.
IntegrationRequires to point your requests to the mocked server endpoint.Does not require any changes to an existing code.
DefinitionMocks are defined using a custom JSON file, being a separate context.Mocks are defined in a client-side JavaScript, allowing to reuse actual logic, typings, utilities.

JSON Server is an actual HTTP server based on an abstract definition. Mock Service Worker is a Service Worker intercepting actual requests your application makes.

MirageJS

MirageJS is a mocking library that is highly focused on data relation, and allows to model dynamic scenarios similar to those of an actual production server.

CriteriaMirageJSMock Service Worker
Supported APIRESTREST / GraphQL
EnvironmentsBrowserNode / Browser
ImplementationIntercepts requests on the application level by stubbing native fetch and XMLHttpRequest.Intercepts requests on the network level using Service Worker API.
IntegrationDoes not require any changes to an existing code.Does not require any changes to an existing code.
DefinitionUses OOP and ORM oriented declaration of mocks. Provides rich tools for data modelling.Uses functional declaration of routes and responses. Non-opinionated in regards to data modelling.

MirageJS has a focus on data relation during mocking. Mock Service Worker, on the other hand, conducts API mocking on per-transaction basis, making mocking a matter of request-response relation.

Service Mocker

Service Mocker is another library that leverages the Service Worker API for the purpose of mocking. Although both libraries share a similar idea, their implementations differ.

CriteriaService MockerMock Service Worker
Supported APIRESTREST / GraphQL
EnvironmentsBrowserNode / Browser
ImplementationRegisters a Service Worker. Resolves mocked responses as a part of the worker.Registers a Service Worker. Resolves mocked responses on the client side.
IntegrationRequires to have the build worker file at the proper scope. May require sw-loader, or configuring the Service-Worker-Allowed header on the server.Does not require any changes to the build setup.
DefinitionMocking logic is written in a separate server.js file (in a Service Worker's context), dettached from your app's logic.Mocks are defined in a client-side JavaScript, allowing to reuse actual app's logic.

Service Worker operation

Since both libraries operate using Service Worker, it's resonable to compare the difference in how they handle their worker instance.

Scope

Service Worker is registered for a specific scope on the website. For example, a worker registered on a /products path cannot affect anything happening on a /contacts page. The scope of a worker is defined by the location of the worker file.

Service MockerMock Service Worker
Leverages a dedicated Service-Worker-Allowed header to control a worker's scope. Requires access to the build configuration to set the header.Encourages to register a Service Worker in the root of your application (by running npx msw init shorthand command).

Cookies

Setting cookies on a mocked Response instance is forbidden by security reasons.

Service MockerMock Service Worker
No support for setting cookies on a mocked response.Imitates setting cookies on a response by writing to document.cookie directly. Read more about mocking cookies.

Hard reload

Per specification, a hard reload of a page stops any Service Worker execution until the next reload. Needless to say, this results into an unpleasant mocking experience.

Service MockerMock Service Worker
Does not handle hard reload.Overcomes this issue by scheduling a regular location.reload() once a hard reload is detected.

Multi-project support

Since Service Worker controls a page at a given host and port, opening a different project on the same port may result into Service Worker still attempting to process irrelevant requests.

Service MockerMock Service Worker
Does not support multiple projects on the same port. Requires to unregister the Service Worker manually in your browser.Supports multiple projects on the same port by detecting when the last client is closed, unregistering the Service Worker automatically.