Please don't mock SWR, Fetch or Axios in your unit/integration tests.
In this video you will learn how to test components that use "useSWR" without doing jest.mock("swr") or jest.mock("axios") or jest.spyOn(window, 'fetch') in order to give you more confidence on your tests :)
We are going to use MSW (Mock Service Worker) in order to mock the HTTP call itself, not your favorite fetching library
Top comments (3)
Is this still the best case to mock requests in jest ? I am using
axios
notfetch
inswr
.Is there any cons to use
jest.spyOn
?I dont want to download extra dev deps
msw
andwhatwg-fetch
if thespyOn
also works.A huge help, Bruno! Loving this series, and some of the best out there for React testing! This area has a HUGE void, and I’m glad you’re filling it!
Thank you Braydon :)