Challenge: When localStorage leaks between tests

Ideally when you write your tests, each test can be run by itself in isolation. And you should be able to run your tests in any order. (There is an option in Jest to run your tests in a random order to help enforce this).

But sometimes things from previous tests leak into other tests. They might set a global variable, leave some mock/spied on functions, or mutate data.

In this test challenge we have a simple todo app that we are testing. It uses window.localStorage in the browser to read/write some data.

And if you haven't guessed already - this is leaking into other tests.

i
Challenge Mode: This lesson is actually a challenge - you are given a half working test and have to figure out how to get it working correctly.

You can use our courses to learn how to solve this challenge - there are courses on everything from basic Jest/Vitest fundamentals, down to how to test frontend code (using React Testing Library).

Loading full lesson content & code...

Ready to try running tests for this lesson?