mswMock Service Worker
  1. Api
  2. setupWorker()

setupWorker()

A function that configures a Service Worker instance with a given mock definition, and returns the API to control that worker instance.

Examples

The setupWorker() function is used for client-side mocking. You usually create a worker instance and activate it by calling the start() method on the returned API.

1import { setupWorker, rest } from 'msw'
2
3const worker = setupWorker(
4 // Provide request handlers
5 rest.get('/user/:userId', (req, res, ctx) => {
6 return res(
7 ctx.json({
8 firstName: 'John',
9 lastName: 'Maverick',
10 }),
11 )
12 }),
13)
14
15// Start the Mock Service Worker
16worker.start()