bypass

Perform an additional request outside of the interception algorithm.

Call signature

The same as the globalThis.fetch() function, the bypass function expects two arguments: RequestInput and RequestInit. It returns a tuple of modified input and init arguments to then be provided on the regular globalThis.fetch() call.

function bypass(input: RequestInput, init?: RequestInit): Request {}

bypass.ts

Source code for the `bypass` namespace.

Usage

This method is designed to perform HTTP requests outside of the library’s interception algorithm. Requests performed via bypass() will never be intercepted, even if there are otherwise matching request handlers present in the network description. This special behavior enables more complex network scenarios, such as Response patching:

import { http, bypass, HttpResponse } from 'msw'
 
export const handlers = [
  http.get('/user', async ({ request }) => {
    // Perform the intercepted "GET /user" request as-is
    // by passing its "request" reference to the "bypass" function.
    const response = await fetch(bypass(request))
    const realUser = await response.json()
 
    // Return a mocked JSON response by patching the original response
    // together with a mocked data.
    return HttpResponse.json({
      ...realUser,
      lastName: 'Maverick',
    })
  }),
]

Unlike passthrough(), the bypass() function results in an additional request being made. You can think of it as a server requesting additional resources while handling a request. Because of this, bypass() must not be used when all you wish is to perform the intercepted request as-is (use passthrough() in that case).