eugef / node-mocks-http

Mock 'http' objects for testing Express,js, Next.js and Koa routing functions
Other
755 stars 134 forks source link

Question: How to mock an request error? #296

Closed mlshohet closed 6 months ago

mlshohet commented 6 months ago

Hello, I was wondering if you knew a technique to mock an error on the request? I need to test if the response is an error.

Love the library, such a great job!

Thanks for any help!

eugef commented 6 months ago

Hi @mlshohet can you give an example what kind of error in request do you want to mock?

mlshohet commented 6 months ago

Hi @eugef, thank you so much for the reply! I'm sorry about the bother. I was looking how to test for network errors. I figured out if there's no body on response I can just throw an exception or send an error code. Then I could catch the error, or check for this error code and handle it. This works for the tests. Best!