mswMock Service Worker
  1. Api
  2. setupServer()
  3. restoreHandlers()

restoreHandlers()

Marks all used one-time request handlers as unused, allowing them to affect network again.

Examples

1import { rest } from 'msw'
2import { setupServer } from 'msw/node'
3
4const server = setupServer(
5 rest.get('/book/:bookId', (req, res, ctx) => {
6 return res(ctx.json({ title: 'Lord of the Rings' }))
7 }),
8)
9
10beforeAll(() => {
11 server.listen()
12})
13
14afterAll(() => {
15 server.close()
16})
17
18test('handles server error gracefully', () => {
19 server.use(
20 rest.get('/book/:bookId', (req, res, ctx) => {
21 return res.once(
22 ctx.status(500),
23 ctx.json({ message: 'Internal server error' }),
24 )
25 }),
26 )
27
28 // Assert your application's behavior given server error.
29})
30
31test('renders book detail', () => {
32 // At this point the server error request handler has been used,
33 // and will not affect network anymore.
34})
35
36test('handles server error when I posted a book review', () => {
37 // Restore one-time request handlers.
38 // A "GET /book/:bookId" request will return a mocked server error
39 // only for the next book detail request.
40 server.restoreHandlers()
41})