Skip to content

Instantly share code, notes, and snippets.

@Dromediansk
Created June 13, 2020 14:13
Show Gist options
  • Save Dromediansk/8bffa37b1f000b18e1d093adad04887f to your computer and use it in GitHub Desktop.
Save Dromediansk/8bffa37b1f000b18e1d093adad04887f to your computer and use it in GitHub Desktop.
Handling errors in useFetch
it("should catch error", async () => {
jest.spyOn(global, "fetch").mockImplementation(() =>
Promise.resolve({
json: () => Promise.reject("oops, error occured!"),
})
);
const { result, waitForNextUpdate } = renderHook(() =>
useFetch(stubbedFetchUrl, { current: true }, [])
);
await waitForNextUpdate();
expect(result.current).toStrictEqual({
loading: false,
data: [],
error: "oops, error occured!",
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment