LOGOMock 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

With restoreHandlers

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 error' }),
11 )
12 }),
13 )
14 })
15
16 cy.visit('/book/abc-123')
17 })
18
19 afterEach(() => {
20 return cy.window().then((window) => {
21 const { worker } = window
22 worker.restoreHandlers()
23 })
24 })
25
26 test('handles server error in the UI', () => {
27 // Initial page load will trigger a one-time request handler override
28 // to "GET /book/:bookId" request handler declared in the
29 // `before` hook above.
30 })
31
32 test('renders book details', () => {
33 // Requests to "GET /book/:bookId" in this test will trigger a one-time request handler override
34 // again, because the `.restoreHandlers()` call restore to a unused one-time request handler
35 })
36})