- Introduction
- Getting started
- Philosophy
- Comparison
- Limitations
- Debugging runbook
- FAQ
- Basics
- Concepts
- Network behavior
- Integrations
- API
- CLI
- Best practices
- Recipes
- Cookies
- Query parameters
- Response patching
- Polling
- Streaming
- Network errors
- File uploads
- Responding with binary
- Custom worker script location
- Global response delay
- GraphQL query batching
- Higher-order resolver
- Keeping mocks in sync
- Merging Service Workers
- Mock GraphQL schema
- Remote Request Interception
- Using CDN
- Using custom "homepage" property
- Using local HTTPS
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"}