- 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
- Using CDN
- Using custom "homepage" property
- Using local HTTPS
delay
Control response timing.
By default, MSW does not delay any mocked responses, so they arrive nearly instantaneously. This behavior may not be desired in certain scenarios, like testing server latency or request timeouts, as it doesn’t reflect the real-world client-server communication.
Call signature
function delay(duration?: number): Promise<void> {}
function delay(mode?: 'real' | 'infinite'): Promise<void> {}
delay.ts
Source code for the `delay` function.
Implicit delay
When invoked without any arguments, the delay
function applies a realistic server response time. It’s a random number equal to the average response time you encounter when communicating with an actual HTTP server (~100-400ms).
import { http, delay, HttpResponse } from 'msw'
export const handlers = [
http.put('/books/:bookId', async () => {
// Await a random realistic server response time.
await delay()
return HttpResponse.json({ id: 'abc-123' })
}),
]
Note that when testing in Node.js, the implicit delay is negated to prevent it affecting the test performance. If you wish to delay responses in Node.js, please use one of the options below.
Explicit delay
You can provide an exact delay duration in milliseconds:
import { http, delay, HttpResponse } from 'msw'
export const handlers = [
http.get('/user', async () => {
// Wait for 1000ms before responding.
await delay(1000)
return new HttpResponse(null, { status: 404 })
}),
]
Controlling precise delay timing is handy when mocking response streams:
import { http, delay, HttpResponse } from 'msw'
export const handlers = [
http.get('/video', () => {
const stream = new ReadableStream({
async start() {
controller.enqueue(new Uint8Array([1, 2, 3]))
await delay(1000)
controller.enqueue(new Uint8Array([4, 5, 6]))
await delay(200)
controller.enqueue(new Uint8Array([7, 8, 9]))
controller.close()
},
})
return new HttpResponse(stream, {
headers: {
'Content-Type': 'video/mp4',
},
})
}),
]
Delay modes
The delay
function also accepts a string that’s an enum representing a delay mode:
"real"
, explicitly sets the realistic response time;"infinite"
, indefinitely delays the response, making it pend forever.
Delay modes are useful to test certain server response scenarios. For example, by using the “infinite” mode, you can test how your application handles response timeouts:
import { http, delay } from 'msw'
export const handlers = [
http.get('/book/:bookId', async () => {
// This request will hang indefinitely.
await delay('infinite')
// And this response will never be sent.
return new Response()
}),
]