DEV Community

Bruno Antunes
Bruno Antunes

Posted on

12 2

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 :)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay