DEV Community

Bruno Antunes
Bruno Antunes

Posted on

Please don't mock SWR, Fetch or Axios in your unit/integration tests

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)

Collapse
 
kdssoftware profile image
KDS SOFTWARE

Is this still the best case to mock requests in jest ? I am using axios not fetch in swr.
Is there any cons to use jest.spyOn?
I dont want to download extra dev deps msw and whatwg-fetch if the spyOn also works.

Collapse
 
braydoncoyer profile image
Braydon Coyer

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!

Collapse
 
bmvantunes profile image
Bruno Antunes

Thank you Braydon :)