How to check raising error by !response.ok ?
See original GitHub issuejest-fetch-mock v3.0.1
Can’t catch error =(
PointSvc.fetchData has code:
const resp = await fetch(`/someUrl/?search=${params.label}`);
if (!resp.ok)
throw new Error(resp.statusText);
PointSvc.spec.ts:
expect.assertions(1);
const statusText = 'Shit happens!';
fetchMock.mockResponseOnce('fail', {
headers: { 'content-type': 'text/plain; charset=UTF-8' },
status: 401,
statusText,
});
expect(() => {
PointSvc.fetchData({ label: 'anysearch' });
})
.toThrow(statusText);
Result:
Received function did not throw
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
Fetch - Error Handling for Failed HTTP Responses and ...
For HTTP errors we can check the response.ok property to see if the request failed and reject the promise ourselves by calling return ......
Read more >Handling Failed HTTP Responses With fetch() - TJ VanToll
Per MDN, the fetch() API only rejects a promise when a “network error is encountered, although this usually means permissions issues or similar....
Read more >Fetch: reject promise and catch the error if status is not OK?
Check response.ok; reject if not OK, instead of throw an error; Further process any error hints from server, e.g. validation issues.
Read more >Response.ok - Web APIs - MDN Web Docs
The ok read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range ...
Read more >When That's Not So Fetch: Error Handling With fetch()
The fetch API provides an ok property to the Promise response which indicates whether the HTTP status is within the range 200-299 (inclusive)....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
or
Jest has built in functions to handle resolution or rejection of promises
@viT-1 I used
and that worked out.