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'23const worker = setupWorker(4 // Provide request handlers5 rest.get('/user/:userId', (req, res, ctx) => {6 return res(7 ctx.json({8 firstName: 'John',9 lastName: 'Maverick',10 }),11 )12 }),13)1415// Start the Mock Service Worker16worker.start()