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

restoreHandlers()

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

Examples

1describe('Book detail', () => {
2 before(() => {
3 cy.window().then((window) => {
4 const { worker, rest } = window.msw
5
6 worker.use(
7 rest.get('/book/:bookId', (req, res, ctx) => {
8 return res.once(
9 ctx.status(500),
10 ctx.json({ message: 'Internal server erro' }),
11 )
12 }),
13 )
14 })
15
16 cy.visit('/book/abc-123')
17 })
18
19 test('handles server error in the UI', () => {
20 // Initial page load will trigger a one-time request handler override
21 // to "GET /book/:bookId" request handler declared in the
22 // `before` hook above.
23 })
24
25 test('renders book details', () => {
26 // Requests to "GET /book/:bookId" in this test will return
27 // a mocked book details, because the server error request handler
28 // is a one-time handler that has been already triggered.
29 })
30})