Skip to content

What is the best way to mock bypassed fetch in handlers? #1023

Answered by kettanaito
misha-erm asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, @MikeYermolayev.

I recommend not performing any bypassed requests during your tests. There's no reason to: the entire purpose of API mocking is to scope your testing surface to the code/logic you wish to test. Performing an actual request subjects your test runs to all sorts of brittle network behaviors and results in unreliable tests.

The library comes with a built-in way to prevent such bypassed requests from happening during test runs:

server.listen({ onUnhandledRequest: 'error' })

With this option set, whenever there's a request you don't wish to handle, it will throw an exception, halting your test. This is the behavior I'd recommend.

If you wish to mock certain endpoints that d…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@misha-erm
Comment options

@kettanaito
Comment options

@misha-erm
Comment options

Answer selected by misha-erm
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