jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 118 forks source link

allow mock declarations to specify HTTP status code #85

Open tomprogers opened 6 years ago

tomprogers commented 6 years ago

As far as I can tell, there's no way for me to specify the HTTP status code that should be returned by the fake call.

If I'm writing a library that should react intelligently based on the details of a failure scenario, and a good webservice leverages HTTP status codes to signal some nuance, then my test suite will need to be able to emulate responses with 400, 404, 409, 500, etc. It's not clear that mockReject permits that.

The example in the docs includes this:

fetch.mockReject(new Error('fake error message'))

const expectedActions = [
  { type: 'SET_ACCESS_TOKEN_FAILED', error: { status: 503 } }
]

But it's not clear how the mockReject call is able to specify 503. I dug through the source a bit and couldn't spot any wiring between the arguments to mock and the status code being returned.

jefflau commented 6 years ago

You should be able to specify it in the Response object or in the Error object. In this module it gives you the ability to mock the response. For a reject you get an error object and for a resolve, you get a response object.

Depending on how you deal with your fetch error status codes you can deal with it as an error which you threw yourself or in the .then()

https://www.tjvantoll.com/2015/09/13/fetch-and-errors/ https://github.com/github/fetch#caveats

jennatuckerdeveloper commented 5 years ago

I am also facing the same limitation. I have tried various ways of passing status codes to .mockResponses and .mockResponseOnce both, but the status code passed always gets overwritten and returns as 200. The example code for mockResponses includes a status object that seems to be ignored and a default status code of 200 returned. I get the same result with and without including a status object at all. It looks as if the parameter that would enable a user to set a unique status code is not actually support internally, and a default status code is the only one supported. Any chance you could look into this and possibly post a bit of code to show how to successful mock a custom status code? Thanks in advance.

jennatuckerdeveloper commented 5 years ago

The ability to pass a custom status code is supported. I was making a user error in the data type that I passed to .mockResponses. You can mock a custom response status code with that method. Each individual argument is an array. The array includes two elements. The first element is a JSON string representing the body. The second element is a plain object {status: 204} or other unique status code. I am leaving my questions and answer here in case someone else hits this snag. The docs attempt to explain the params passed to .mockResponses, but I think some clarification might help prevent user error and perhaps showing examples that include custom status codes (right now they are all 200) might highlight the existence of this feature. Thanks.

tomprogers commented 3 years ago

I think this ticket can be closed. The current iteration of the docs addresses my original concerns. Kudos! And thanks.