Test Isolation with React

Photo by Mika Matin on Unsplash

Why your tests should be completely isolated from one another and how to do that.

The inspiration for this blogpost comes from seeing React tests that look like this:

1 const utils = render ( < Foo /> ) 2 3 test ( 'test 1' , ( ) => { 4 5 } ) 6 7 test ( 'test 2' , ( ) => { 8 9 } )

So I want to talk about the importance of test isolation and guide you to a better way to write your tests to improve the reliability of the tests, simplify the code, and increase the confidence your tests and provide as well.

Let's take this simple component as an example:

1 import React , { useRef } from 'react' 2 3 function Counter ( props ) { 4 const initialProps = useRef ( props ) . current 5 const { initialCount = 0 , maxClicks = 3 } = props 6 7 const [ count , setCount ] = React . useState ( initialCount ) 8 const tooMany = count >= maxClicks 9 10 const handleReset = ( ) => setCount ( initialProps . initialCount ) 11 const handleClick = ( ) => setCount ( currentCount => currentCount + 1 ) 12 13 return ( 14 < div > 15 < button onClick = { handleClick } disabled = { tooMany } > 16 Count : { count } 17 </ button > 18 { tooMany ? < button onClick = { handleReset } > reset </ button > : null } 19 </ div > 20 ) 21 } 22 23 export { Counter }

Here's a rendered version of the component:

Our first test suite

Let's start with a test suite like the one that inspired this post:

1 2 import '@testing-library/jest-dom/extend-expect' 3 import { render } from '@testing-library/react' 4 import userEvent from '@testing-library/user-event' 5 import React from 'react' 6 7 import { Counter } from '../counter' 8 9 const { getByText } = render ( < Counter maxClicks = { 4 } initialCount = { 3 } /> ) 10 const counterButton = getByText ( /^count/i ) 11 12 test ( 'the counter is initialized to the initialCount' , ( ) => { 13 expect ( counterButton ) . toHaveTextContent ( '3' ) 14 } ) 15 16 test ( 'when clicked, the counter increments the click' , ( ) => { 17 userEvent . click ( counterButton ) 18 expect ( counterButton ) . toHaveTextContent ( '4' ) 19 } ) 20 21 test ( ` the counter button is disabled when it's hit the maxClicks ` , ( ) => { 22 userEvent . click ( counterButton ) 23 expect ( counterButton ) . toHaveAttribute ( 'disabled' ) 24 } ) 25 26 test ( ` the counter button does not increment the count when clicked when it's hit the maxClicks ` , ( ) => { 27 expect ( counterButton ) . toHaveTextContent ( '4' ) 28 } ) 29 30 test ( ` the reset button has been rendered and resets the count when it's hit the maxClicks ` , ( ) => { 31 userEvent . click ( getByText ( /reset/i ) ) 32 expect ( counterButton ) . toHaveTextContent ( '3' ) 33 } )

First of all, as of @testing-library/react@9.0.0 this style of testing won't even work properly, but let's imagine that it would.

These tests give us 100% coverage of the component and verify exactly what they say they'll verify. The problem is that they share mutable state. What is the mutable state they're sharing? The component! One test clicks the counter button and the other tests rely on that fact to pass. If we were to delete (or .skip ) the test called "when clicked, the counter increments the click" it would break all the following tests:

This is a problem because it means that we can't reliably refactor these tests, or run a single test in isolation of the others for debugging purposes because we don't know which tests are impacting the functionality of others. It can be really confusing when someone comes in to make changes to one test and other tests start breaking out of nowhere.

Better

So let's try something else and see how that changes things:

1 import '@testing-library/jest-dom/extend-expect' 2 import { render } from '@testing-library/react' 3 import userEvent from '@testing-library/user-event' 4 import React from 'react' 5 6 import { Counter } from '../counter' 7 8 let getByText , counterButton 9 10 beforeEach ( ( ) => { 11 const utils = render ( < Counter maxClicks = { 4 } initialCount = { 3 } /> ) 12 getByText = utils . getByText 13 counterButton = utils . getByText ( /^count/i ) 14 } ) 15 16 test ( 'the counter is initialized to the initialCount' , ( ) => { 17 expect ( counterButton ) . toHaveTextContent ( '3' ) 18 } ) 19 20 test ( 'when clicked, the counter increments the click' , ( ) => { 21 userEvent . click ( counterButton ) 22 expect ( counterButton ) . toHaveTextContent ( '4' ) 23 } ) 24 25 test ( ` the counter button is disabled when it's hit the maxClicks ` , ( ) => { 26 userEvent . click ( counterButton ) 27 expect ( counterButton ) . toHaveAttribute ( 'disabled' ) 28 } ) 29 30 test ( ` the counter button does not increment the count when clicked when it's hit the maxClicks ` , ( ) => { 31 userEvent . click ( counterButton ) 32 userEvent . click ( counterButton ) 33 expect ( counterButton ) . toHaveTextContent ( '4' ) 34 } ) 35 36 test ( ` the reset button has been rendered and resets the count when it's hit the maxClicks ` , ( ) => { 37 userEvent . click ( counterButton ) 38 userEvent . click ( getByText ( /reset/i ) ) 39 expect ( counterButton ) . toHaveTextContent ( '3' ) 40 } )

With this, each test is completely isolated from the other. We can delete or skip any test and the rest of the tests continue to pass. The biggest fundamental difference here is that each test has its own count instance to work with and it's unmounted after each test (this happens automatically thanks to React Testing Library). This significantly reduces the amount of complexity of our tests with minor changes.

One thing people often say against this approach is that it's slower than the previous approach. I'm not totally sure how to respond to that... Like, how much slower? Like a few milliseconds? In that case, so what? A few seconds? Then your component should probably be optimized because that's just terrible. I know it adds up over time, but with the added confidence and improved maintainability of this approach, I'd gladly wait an extra few seconds to render things this way. In addition, you shouldn't often have to run the entire test base anyway thanks to great watch mode support like we have in Jest.

Even better

So I'm actually still not super happy with the tests we have above. I'm not a huge fan of beforeEach and sharing variables between tests. I feel like they lead to tests that are harder to understand. Let's try again:

1 import '@testing-library/jest-dom/extend-expect' 2 import { render } from '@testing-library/react' 3 import userEvent from '@testing-library/user-event' 4 import React from 'react' 5 6 import { Counter } from '../counter' 7 8 function renderCounter ( props ) { 9 const utils = render ( < Counter maxClicks = { 4 } initialCount = { 3 } { ... props } /> ) 10 const counterButton = utils . getByText ( /^count/i ) 11 return { ... utils , counterButton } 12 } 13 14 test ( 'the counter is initialized to the initialCount' , ( ) => { 15 const { counterButton } = renderCounter ( ) 16 expect ( counterButton ) . toHaveTextContent ( '3' ) 17 } ) 18 19 test ( 'when clicked, the counter increments the click' , ( ) => { 20 const { counterButton } = renderCounter ( ) 21 userEvent . click ( counterButton ) 22 expect ( counterButton ) . toHaveTextContent ( '4' ) 23 } ) 24 25 test ( ` the counter button is disabled when it's hit the maxClicks ` , ( ) => { 26 const { counterButton } = renderCounter ( { 27 maxClicks : 4 , 28 initialCount : 4 , 29 } ) 30 expect ( counterButton ) . toHaveAttribute ( 'disabled' ) 31 } ) 32 33 test ( ` the counter button does not increment the count when clicked when it's hit the maxClicks ` , ( ) => { 34 const { counterButton } = renderCounter ( { 35 maxClicks : 4 , 36 initialCount : 4 , 37 } ) 38 userEvent . click ( counterButton ) 39 expect ( counterButton ) . toHaveTextContent ( '4' ) 40 } ) 41 42 test ( ` the reset button has been rendered and resets the count when it's hit the maxClicks ` , ( ) => { 43 const { getByText , counterButton } = renderCounter ( ) 44 userEvent . click ( counterButton ) 45 userEvent . click ( getByText ( /reset/i ) ) 46 expect ( counterButton ) . toHaveTextContent ( '3' ) 47 } )

Here we've increased some boilerplate, but now every test is not only isolated technically, but also visually. You can look at a test and see exactly what it does without having to worry about what hooks are happening within the test. This is a big win in the ability for you to be able to refactor, remove, or add to the tests.

Even better better

I like what we have now, but I think we need to take things one step further before I feel really happy about things. We've split our tests up by functionality, but what we really want to have confidence in is the use case that our component satisfies. It allows clicks until the maxClicks is reached, then requires a reset. That's what we're trying to verify and gain confidence in. I'm much more interested in use cases when I'm testing than specific functionality. So what would these tests look like if we concerned ourselves more with the use case than the individual functionality?

1 import '@testing-library/jest-dom/extend-expect' 2 import { render } from '@testing-library/react' 3 import userEvent from '@testing-library/user-event' 4 import React from 'react' 5 6 import { Counter } from '../counter' 7 8 test ( 'allows clicks until the maxClicks is reached, then requires a reset' , ( ) => { 9 const { getByText } = renderIntoDocument ( 10 < Counter maxClicks = { 4 } initialCount = { 3 } /> , 11 ) 12 const counterButton = getByText ( /^count/i ) 13 14 15 expect ( counterButton ) . toHaveTextContent ( '3' ) 16 17 18 userEvent . click ( counterButton ) 19 expect ( counterButton ) . toHaveTextContent ( '4' ) 20 21 22 expect ( counterButton ) . toHaveAttribute ( 'disabled' ) 23 24 userEvent . click ( counterButton ) 25 expect ( counterButton ) . toHaveTextContent ( '4' ) 26 27 28 userEvent . click ( getByText ( /reset/i ) ) 29 30 31 expect ( counterButton ) . toHaveTextContent ( '3' ) 32 33 34 userEvent . click ( counterButton ) 35 expect ( counterButton ) . toHaveTextContent ( '4' ) 36 } )

I really love this kind of test. It helps me avoid thinking about functionality and focus more on what I'm trying to accomplish with the component. It serves as much better documentation of the component than the other tests as well.

In the past, the reason we wouldn't do this (have multiple assertions in a single test) is because it was hard to tell which part of the test broke. But now we have much better error output and it's really easy to identify what part of the test broke. For example:

The code frame is especially helpful. It shows not only the line number, but the code around the failed assertion which shows our comments and other code to really help give us context around the error message that not even our previous tests gave us.

I should mention, this isn't to say that you shouldn't separate test cases for a component! There are many reasons you'd want to do that and most of the time you will. Just focus more on use cases than functionality and you'll generally cover most of the code you care about with that. Then you can have a few extra tests to handle edge cases.

Conclusion

I hope this is helpful to you! You can find the code for this example here. Try to keep your tests isolated from one another and focus on use cases rather than functionality and you'll have a much better time testing! Good luck!