getResponse

Resolve a request against request handlers programmatically.

The getResponse function is designed for programmatic resolution of requests. You don’t need this function to use MSW for development and testing. You may need this function when building custom packages and functionality on top of MSW. You may also need this function for more complex request flows, such as mocking batched GraphQL queries.

Call signature

function getResponse(
  handlers: Array<RequestHandler>,
  request: Request
): Promise<Response | undefined>

getResponse.ts

Source code for the `getResponse` function.

Usage

import { http, HttpResponse, getResponse } from 'msw'
 
const handlers = [
  http.get('http://localhost/user', () => {
    return HttpResponse.json({ name: 'John' })
  }),
]
const request = new Request('http://localhost/user')
 
const response = await getResponse(handlers, request)
const user = await response?.json()
// {"name":"John"}