Closed michaelknoch closed 5 years ago
When you say it does not seem to work as expected - what happens?
jest-fetch-mock allows you pass a Response object, so whatever you usually get in that you should be able to mock.
it was my bad @jefflau, i havent seen the second parameter
fetch.mockResponseOnce(JSON.stringify("fail"), { status: 401, ok: false });
This does not work for me.
fetch.once(JSON.stringify('testdata'), {ok: false});
results in:
Argument of type '{ok: boolean;}' is not assignable to parameter of type 'MockParams'. Object literal may only specify known properties, and 'ok' does not exist in type 'MockParams';
Thoughts?
This works for me; apparently ok
gets set to false
based on the status code.
fetchMock.mockResponseOnce(
JSON.stringify('Ignore this'),
{ status: 500, statusText: 'Server error' },
);
I did not realy get how to mock a realistic fetch failing behaviour. As described in this article fetch only rejects promises if network errors happen. So for example if the server returns a
500
, fetch normally would resolve a promise with{response: { ok: false }}
.Is it possible to mock such a response?
does not seem to work as expected