jameslnewell / xhr-mock

Utility for mocking XMLHttpRequest.
196 stars 48 forks source link

What if I'm expecting an error? #70

Closed jonathansp closed 6 years ago

jonathansp commented 6 years ago

In my test case I'm expecting an HTTP error, so I mocked XHR response like this:

    xhrmock.get('dummy.host/schemas', {
      status: 500,
      reason: 'Internal Server Error',
      body: 'Internal Server Error',
    })

How to avoid the follow message to be printed?

 PASS  tests/schemas.spec.js
 Console
    console.error node_modules/xhr-mock/lib/MockXMLHttpRequest.js:674
      xhr-mock: No handler returned a response for the request.

        GET dummy.host/schemas HTTP/1.1

https://github.com/jameslnewell/xhr-mock/blob/master/packages/xhr-mock/src/MockXMLHttpRequest.ts#L73

jameslnewell commented 6 years ago

Hmmm... it shouldn't be showing that message if your mock is registered before the request and your request is being matched e.g. https://codesandbox.io/s/n05m1wl1jl

Can you please check the mock is registered before the request is made and that the request URL matches the mock URL e.g. dummy.host/schemas not http://dummy.host/schemas

jonathansp commented 6 years ago

The mock is registered before the request is done. I've just realized it only happens when I use jest --watch.

jameslnewell commented 6 years ago

Are you calling xhrmock.teardown() after each test? If you already are, can you please provide minimal repository reproducing the problem. Thanks.