mswMock Service Worker
  1. Api
  2. context
  3. fetch()

fetch()

Performs a request inside a request handler.

Response patching

The ctx.fetch() utility is primarily designed for Response patching, as it produces requests that are bypassed by any otherwise matching request handlers.

NodeJS compatibility

To preserve a NodeJS compatibility, ctx.fetch() uses node-fetch package when called in the NodeJS environment.

Call signature

1function fetch(req: MockedRequest): Promise<any>
2function fetch(input: RequestInfo, init?: RequestInit)

Examples

1rest.get('https://api.github.com/users/:username', async (req, res, ctx) => {
2 // Performs an original request to the GitHub API.
3 const originalResponse = await ctx.fetch(req)
4
5 return res(
6 ctx.json({
7 name: 'John Maverick',
8 location: originalResponse.location,
9 }),
10 )
11})