Skip to content

How to provoke an error and success response on the same GET endpoint #783

Answered by kettanaito
kevinmamaqi asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, @kevinmamaqi. You can use a runtime request handler to override which response is being returned for the same endpoint to test multiple states of your code.

import { rest } from 'msw'
import { setupServer } from 'msw/node'

const server = setupServer(
  // I recommend listing the success scenarios when declaring the server.
  rest.get('/resource', (req, res, ctx) => {
    return res(ctx.json({ id: 1 }))
  })
)

beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

it('handles a successful response', () => {
  // run your code (i.e. render components, execute functions)
  // without any additions, this test case will receive a success…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kevinmamaqi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #782 on June 15, 2021 21:19.