Skip to content
Learn best testing practices from the MSW creator Enroll now

The industry standard for API mocking in JavaScript.

Mock Service Worker is an API mocking library that allows you to write client-agnostic mocks and reuse them across any frameworks, tools, and environments.

ts
import { 
http
,
HttpResponse
} from 'msw/http'
import {
setupWorker
} from 'msw/browser'
// Describe the network once. const
handlers
= [
http
.
get
<{
id
: string }>('https://acme.com/product/:id', ({
params
}) => {
return
HttpResponse
.
json
({
id
:
params
.
id
,
title
: 'Porcelain Mug',
price
: 9.99,
}) }), ] // Integrate API mocking anywhere. const
worker
=
setupWorker
(...
handlers
)
await
worker
.
start
()

Trusted by teams at

API mocking that feels like an extension of JavaScript.

Standard-first API mocking

MSW doesn't reinvent the wheel. How do you handle the network in JavaScript? With the Fetch API! And that's precisely how you write your mocks.

ts
http.post('/user/:id', async ({ request, params }) => {
  const user = await request.clone().json()
  const headers = request.headers
  headers.set('x-mocked', 'true')

  return Response.json(
    { ...user, id: params.id },
    { headers },
  )
})

Describe the network once

The era of patching window.fetch is over. MSW pioneered transparent network interception where you describe your mocks once and they work everywhere.

ts
export const handlers = [
  http.get('/user', () => {
    return HttpResponse.json({ id: 1 })
  }),
]

// One mock that works everywhere.
fetch('/user')
axios.get('/user')
useSWR('/user')

Unmatched feature set

Mock REST, GraphQL, Server-Sent Events, and WebSocket APIs with a single network contract, simultaneously.

ts
const github = graphql.link('https://api.github.com/graphql')
const binance = ws.link('wss://stream.binance.com/ws')

export const handlers = [
  http.get('https://api.stripe.com/v1/balance', resolver),
  github.query('GetRepository', resolver),
  sse('https://api.openai.com/v1/responses', resolver),
  binance.addEventListener('connection', resolver),
]

API mocking that actually lets requests happen

A decade worth of research to bring you the network interception algorithm that combines the impossible: actually perform requests while giving you full control over them.

Browser

Service Worker API

In the browser, MSW leverages the standard Service Worker API to seamlessly intercept production requests.

  1. fetch()Request client
  2. Service WorkerBrowser API
  3. NetworkBrowser networking
Request with MSW Request without MSW

Node.js

@mswjs/interceptors

In Node.js, MSW intercepts raw socket connections at the lowest possible level before they reach the actual network code written in C.

  1. fetch()Request client
  2. net.connect()Node.js core
  3. net.SocketStream
  4. TCP/TLS wrapsJS bindings for C
  5. NetworkOperating system
Request with MSW Request without MSW

API mocking as a standalone layer for component testing

Become in charge of the network across the entire stack.

Explore integrations

js
// vitest.setup.js
import { setupServer } from 'msw/node'
import { handlers } from './src/mocks/handlers'

const server = setupServer(...handlers)

beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

API mocking that can do more.

API mocking beloved by all who ship quality apps.

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

Kent C. Dodds

Software Engineer and Educator

Mock Service Worker has become a fundamental part of my development and testing workflow. With MSW I don't have to worry about endpoints or databases being down or slow. And I can forget about brittle tests due to changing data. I configure mocks that are 100% reliable and predictable. The result? Faster development and rock-solid automated UI tests.
Cory House

Cory House

Software Architect

I've spent numerous hours mocking and stubbing data for tests over the years wishing for a better approach. I was recently introduced to MSW and love how it instantly improves the developer experience.

Alffrey Chemmannoor

Software Engineer

We've switched our tests over to use Mock Service Worker and we haven't looked back ever since.

Michael Haglund

Engineering Manager

I don't think I can use anything other than MSW after picking it up for a project at my previous workplace, to be honest.

Brady Pascoe

Maintainer of React Bootstrap

Mock Service Worker is the best thing that ever happened to the JS community. Sharing mocks between development, unit, and E2E tests has never been that easy.

Tobias Pickel

Software Engineer

Just a shoutout to MSW for being an absolute dream to work with. Simple to get running and it's allowed me to continue working while the backend was down. Much faster than writing a mock server.

Konna Buraun

Software Engineer

I can't even imagine the mock I'd have to contrive to simulate a server sending a CSRF cookie, and with MSW I don't have to!

Derek DeHart

Software Engineer

Mock Service Worker was a life-saver for me when we had a strict deadline and there was no backend.

Rafal Rudol

Senior Frontend Developer

It's quite a genius use of Service Workers—works for both REST and GraphQL API.

Heitor Lessa

AWS EMEA

When it comes to mocking, I always recommend Mock Service Worker. It's amazing and as close as it gets to plug & play.

Matan Borenkraout

Frontend Engineer

Ship Better Products Today

Mock Service Worker is the best way to integrate API mocking across your entire stack. Test, prototype, and debug without sacrificing your application's integrity. Give it a try, it's open-source and free!

Mock Service Worker logo