useSWR makes developing performant and complex React applications a snap. In many cases, it can remove the need for a global state management solution like Redux or MobX.

We previously covered useSWR on Frontend Digest, but this time we want to take a deeper dive into using useSWR with React Testing Library.

One of my favourites things about SWR is it’s caching solution. However, this global caching system doesn’t play nicely with testing. We don’t want to leak state between tests. When we are testing our React applications, we want clean and predictable results.

In order to use SWR together with Testing Library, we need to clear this cache between each test run. We can do this in our setupTests file.

#testing #jest #react

Two Tricks to Testing with Jest and useSWR
60.85 GEEK