mswMock Service Worker

once()

When called, marks the current request handler as used. Any subsequent request matches to this handler are ignored, and fall-through to the next suitable handler.

Examples

1rest.post('/login', (req, res, ctx) => {
2 return res.once(ctx.json({ id: 'abc-123' }))
3})
1// src/any-module.js
2// The first request to the captured route
3// returns the mocked response specified.
4fetch('/login', { method: 'POST' })
5// { "id": "abc-123" }
6
7// Any subsequent request, although matches,
8// will skip the request handler that has been used.
9// Let's consider that we don't have an actual "POST /login" endpoint.
10fetch('/login', { method: 'POST' })
11// ERR: Network error! (no such endpoint)