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

status()

Sets a given HTTP status code and text (optional) on the response.

Call signature

1function status(code: number, text?: string): MockedResponse

Examples

Status code

1rest.get('/post/:postId', (req, res, ctx) => {
2 // Status text is automatically inferred from a given status code
3 // according to REST API specification.
4 return res(ctx.status(404))
5})

Status code and status text

1rest.get('/post/:postId', (req, res, ctx) => {
2 // You can override the inferred status text
3 // by providing it as the second argument.
4 return res(ctx.status(404, 'Custom status text'))
5})