Skip to content

How to mock an external domain/resource? #986

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

You must be logged in to vote

Hey, @FezVrasta.

You can mock any resources that match the request handler you create. That includes both local and external resources!

Our README features an example of mocking a GET request to GitHub's REST API. You can also read the Request matching page in our docs to learn more about how exactly MSW matches resources.

rest.get('https://github.com/octocat', (req, res, ctx) => {
  return res(
    ctx.delay(1500),
    ctx.status(202, 'Mocked status'),
    ctx.json({
      message: 'Mocked response JSON body',
    }),
  )
}),

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@FezVrasta
Comment options

Answer selected by FezVrasta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants