These libraries address the challenge of developing and testing frontend applications without relying on a live backend. They fall into three main categories: request interception tools that mock network responses directly in the code (msw, nock, axios-mock-adapter), server simulators that spin up a fake REST API (json-server, miragejs), and data generators that create realistic fake data for UI population (faker). Choosing the right tool depends on whether you need browser-level fidelity, Node.js test environment support, or rapid prototyping capabilities.
Building modern web applications often means working before the backend is ready, or testing components without hitting real APIs. The tools axios-mock-adapter, faker, json-server, miragejs, msw, and nock solve this problem in different ways. Some mock the network layer, some spin up fake servers, and others generate the data itself. Let's break down how they work and where they fit in your architecture.
The biggest technical divide is where the mocking happens. Some tools run in the browser like real traffic, while others only work in Node.js test runners.
msw uses Service Workers to intercept requests at the network level in the browser.
// msw: Browser-level interception
import { http, HttpResponse } from 'msw';
import { setupWorker } from 'msw/browser';
const worker = setupWorker(
http.get('/users', () => {
return HttpResponse.json({ id: 1, name: 'Alice' });
})
);
worker.start();
nock intercepts HTTP requests at the Node.js level.
// nock: Node-level interception
import nock from 'nock';
nock('https://api.example.com')
.get('/users')
.reply(200, { id: 1, name: 'Alice' });
axios-mock-adapter hooks directly into Axios instances.
fetch or other HTTP clients.// axios-mock-adapter: Library-specific mocking
import AxiosMockAdapter from 'axios-mock-adapter';
import axios from 'axios';
const mock = new AxiosMockAdapter(axios);
mock.onGet('/users').reply(200, { id: 1, name: 'Alice' });
Some tools spin up an actual HTTP server, while others simulate one inside your application bundle.
json-server runs a standalone Node.js process.
http://localhost:3000).// json-server: CLI usage
// Run in terminal: npx json-server --watch db.json
// db.json
{
"users": [
{ "id": 1, "name": "Alice" }
]
}
miragejs creates a server inside your JavaScript bundle.
// miragejs: In-app server
import { createServer, Model } from 'miragejs';
createServer({
models: {
user: Model
},
seeds(server) {
server.create('user', { name: 'Alice' });
}
});
Mocking responses is one thing, but generating realistic data is another. faker handles the content, while the others handle the transport.
faker (specifically @faker-js/faker) generates fake data on demand.
faker package is deprecated.// faker: Data generation
import { faker } from '@faker-js/faker';
const user = {
id: faker.string.uuid(),
name: faker.person.fullName(),
email: faker.internet.email()
};
msw, nock, and axios-mock-adapter usually return static data unless you combine them with a generator.
// msw with dynamic data
http.get('/users', () => {
return HttpResponse.json({
id: 1,
name: faker.person.fullName()
});
});
Choosing a library also means choosing its future. Some of these tools have shifted maintenance status recently.
faker original package is deprecated.
@faker-js/faker for new projects.// β Deprecated
import { faker } from 'faker';
// β
Recommended
import { faker } from '@faker-js/faker';
miragejs has slowed development.
msw for long-term support.// miragejs: Stable but consider migration
// No specific code change, but plan for potential rewrite
msw is actively maintained.
// msw: Current standard
// Actively updated for React Testing Library, Cypress, etc.
The tool you pick often depends on the type of test you are writing.
You want to test a React component that fetches data. You don't care about the network, just the render.
axios-mock-adapter or msw// axios-mock-adapter example
mock.onGet('/user/1').reply(200, { name: 'Test' });
render(<UserProfile id={1} />);
You want to test the full flow, including network errors and loading states.
msw// msw example for E2E
worker.start({ onUnhandledRequest: 'bypass' });
You are testing a Node.js service that calls external APIs.
nock// nock example
nock('https://external-api.com').get('/data').reply(200, {});
You need a frontend to work before the backend exists.
json-server# json-server CLI
npx json-server --watch db.json --port 4000
| Package | Environment | Type | Maintenance | Best For |
|---|---|---|---|---|
msw | Browser + Node | Network Interceptor | β Active | Integration & E2E Tests |
nock | Node Only | Network Interceptor | β Active | Backend & Node Tests |
axios-mock-adapter | Any (Axios only) | Library Hook | β Active | Quick Unit Tests |
json-server | Node (CLI) | Real Server | β Active | Prototyping |
miragejs | Browser | In-App Server | β οΈ Slow | Legacy Projects |
faker | Any | Data Generator | β Deprecated | Do Not Use (Use @faker-js/faker) |
msw is the modern standard for frontend network mocking. It bridges the gap between unit tests and real network behavior without the overhead of a separate server. If you are starting a new React, Vue, or Svelte project, this should be your default choice.
json-server remains unbeatable for speed when you just need a dummy API running locally. It requires zero code changes in your app, making it perfect for the first week of a project.
nock is the go-to for Node.js developers. If your tests run in Jest or Mocha on the server, nock provides the deepest level of control over HTTP traffic.
faker (specifically @faker-js/faker) is a utility, not a mocker. You will likely use it alongside msw or json-server to make your mock data look real.
Final Thought: Don't mix too many tools. For most frontend teams, a combination of msw for testing and json-server for local development covers 90% of needs. Avoid the deprecated faker package and be cautious with miragejs if you plan for long-term maintenance.
Choose msw (Mock Service Worker) for modern frontend applications that require high-fidelity network mocking directly in the browser and Node.js. It is the best choice for integration and end-to-end testing because it uses the Service Worker API to intercept requests at the network level, matching real production behavior.
Choose nock if you are writing backend tests in Node.js or frontend tests that run in a Node environment (like Jest). It intercepts HTTP requests at the source level in Node, making it powerful for testing server-side logic or API integrations, but it cannot run in the actual browser.
Avoid the original faker package as it is deprecated and unmaintained. Instead, choose its community fork @faker-js/faker if you need to generate realistic fake data for forms, lists, or seeding databases. It is essential for populating UIs during development without relying on real user data.
Choose axios-mock-adapter if your project is tightly coupled to Axios and you need a quick, lightweight way to mock requests within unit tests. It is ideal for isolating specific components without setting up a full network layer, but it locks you into the Axios ecosystem and does not test actual network behavior.
Choose miragejs if you want an in-browser server that intercepts requests without needing a separate process, but be aware that maintenance has slowed. It is suitable for older projects already using it, though new projects should evaluate msw as a more actively maintained alternative for client-side mocking.
Choose json-server when you need a full, running REST API server for prototyping or end-to-end testing without writing backend code. It is perfect for early-stage development where the frontend team needs a persistent data store that supports standard HTTP methods like GET, POST, and PUT.
Industry standard API mocking for JavaScript.
"I found MSW and was thrilled that not only could I still see the mocked responses in my DevTools, but that the mocks didn't have to be written in a Service Worker and could instead live alongside the rest of my app. This made it silly easy to adopt. The fact that I can use it for testing as well makes MSW a huge productivity booster."
β Kent C. Dodds
This README will give you a brief overview of the library, but there's no better place to start with Mock Service Worker than its official documentation.
We've partnered with Egghead to bring you quality paid materials to learn the best practices of API mocking on the web. Please give them a shot! The royalties earned from them help sustain the project's development. Thank you.
In-browser usage is what sets Mock Service Worker apart from other tools. Utilizing the Service Worker API, which can intercept requests for the purpose of caching, Mock Service Worker responds to intercepted requests with your mock definition on the network level. This way your application knows nothing about the mocking.
Take a look at this quick presentation on how Mock Service Worker functions in a browser:
fetch, axios, react-query, you-name-it;// 1. Import the library.
import { http, HttpResponse } from 'msw'
import { setupWorker } from 'msw/browser'
// 2. Describe network behavior with request handlers.
const worker = setupWorker(
http.get('https://github.com/octocat', ({ request, params, cookies }) => {
return HttpResponse.json(
{
message: 'Mocked response',
},
{
status: 202,
statusText: 'Mocked status',
},
)
}),
)
// 3. Start mocking by starting the Service Worker.
await worker.start()
Performing a GET https://github.com/octocat request in your application will result into a mocked response that you can inspect in your browser's "Network" tab:

Tip: Did you know that although Service Worker runs in a separate thread, your request handlers execute entirely on the client? This way you can use the same languages, like TypeScript, third-party libraries, and internal logic to create the mocks you need.
There's no such thing as Service Workers in Node.js. Instead, MSW implements a low-level interception algorithm that can utilize the very same request handlers you have for the browser. This blends the boundary between environments, allowing you to focus on your network behaviors.
fetch, axios, etc. As a result, your tests know nothing about mocking;Here's an example of using Mock Service Worker while developing your Express server:
import express from 'express'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
const app = express()
const server = setupServer()
app.get(
'/checkout/session',
server.boundary((req, res) => {
// Describe the network for this Express route.
server.use(
http.get(
'https://api.stripe.com/v1/checkout/sessions/:id',
({ params }) => {
return HttpResponse.json({
id: params.id,
mode: 'payment',
status: 'open',
})
},
),
)
// Continue with processing the checkout session.
handleSession(req, res)
}),
)
This example showcases
server.boundary()to scope request interception to a particular closure, which is extremely handy!
Mock Service Worker is trusted by hundreds of thousands of engineers around the globe. It's used by companies like Google, Microsoft, Spotify, Amazon, Netflix, and countless others. Despite that, it remains a hobby project maintained in a spare time and has no opportunity to financially support even a single full-time contributor.
You can change that! Consider sponsoring the effort behind one of the most innovative approaches around API mocking. Raise a topic of open source sponsorships with your boss and colleagues. Let's build sustainable open source together!
Become our golden sponsor and get featured right here, enjoying other perks like issue prioritization and a personal consulting session with us.
Learn more on our GitHub Sponsors profile.
|
|
|
|
|
|
|
Become our silver sponsor and get your profile image and link featured right here.
Learn more on our GitHub Sponsors profile.
|
|
|
|
Become our bronze sponsor and get your profile image and link featured in this section.
Learn more on our GitHub Sponsors profile.
|
|
|
|
We've been extremely humbled to receive awards and mentions from the community for all the innovation and reach Mock Service Worker brings to the JavaScript ecosystem.
|
Solution Worth PursuingTechnology Radar (2020β2021) |
|
The Most Exciting Use of TechnologyOpen Source Awards (2020) |