question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to check raising error by !response.ok ?

See original GitHub issue

jest-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:open
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
yinzaracommented, Oct 15, 2020
fetch.mockResponseOnce('{}', { status: 500, headers: { 'content-type': 'application/json' } });`
expect(myFetchFunction()).rejects.toThrow();

or

expect(myFetchFunction()).resolves.toThrowError();

Jest has built in functions to handle resolution or rejection of promises

1reaction
tylervipondcommented, Oct 15, 2020

@viT-1 I used

fetch.mockResponseOnce('{}', { status: 500, headers: { 'content-type': 'application/json' } });`
expect(await myFetchFunction()).toThrowError();

and that worked out.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found