Introducing the react-testing-library 🐐

Photo by Rob Potter on Unsplash

A simpler replacement for enzyme that encourages good testing practices.

Two weeks ago, I wrote a new library! I've been thinking about it for a while. But two weeks ago I started getting pretty serious about it:

I'm seriously starting to think that I should make my own (very small) testing lib and drop enzyme entirely. Most of enzyme's features are not at all useful (and many damaging) to my testbases. I'd rather have something smaller that encourages better practices. — Kent C. Dodds 🛰 (@kentcdodds) March 15, 2018

Read on to get an idea of what I mean by "damaging practices."

Simple and complete React DOM testing utilities that encourage good testing practices.

The problem

You want to write maintainable tests for your React components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your testbase to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.

This solution

The react-testing-library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom and react-dom/test-utils , in a way that encourages better testing practices. Its primary guiding principle is:

The more your tests resemble the way your software is used, the more confidence they can give you. — Kent C. Dodds 🛰 (@kentcdodds) March 23, 2018

So rather than dealing with instances of rendered react components, your tests will work with actual DOM nodes. The utilities this library provides facilitate querying the DOM in the same way the user would. Finding form elements by their label text (just like a user would), finding links and buttons by their text (like a user would). It also exposes a recommended way to find elements by a data-testid as an "escape hatch" for elements where the text content and label do not make sense or is not practical.

This library encourages your applications to be more accessible and allows you to get your tests closer to using your components the way a user will, which allows your tests to give you more confidence that your application will work when a real user uses it.

This library is a replacement for enzyme. While you can follow these guidelines using enzyme itself, enforcing this is harder because of all the extra utilities that enzyme provides (utilities which facilitate testing implementation details). Read more about this in the FAQ.

Also, while the React Testing Library is intended for react-dom, you can use React Native Testing Library which has a very similar API.

What this library is not:

A test runner or framework Specific to a testing framework (though we recommend Jest as our preference, the library works with any framework, and even in codesandbox!)

Examples

Basic Example

1 2 import React from 'react' 3 4 5 6 function HiddenMessage ( { children } ) { 7 const [ showMessage , setShowMessage ] = React . useState ( false ) 8 return ( 9 < div > 10 < label htmlFor = " toggle " > Show Message </ label > 11 < input 12 id = " toggle " 13 type = " checkbox " 14 onChange = { e => setShowMessage ( e . target . checked ) } 15 checked = { showMessage } 16 /> 17 { showMessage ? children : null } 18 </ div > 19 ) 20 } 21 22 export default HiddenMessage 23 24 25 26 27 import '@testing-library/jest-dom/extend-expect' 28 29 30 import React from 'react' 31 import { render , fireEvent } from '@testing-library/react' 32 import HiddenMessage from '../hidden-message' 33 34 test ( 'shows the children when the checkbox is checked' , ( ) => { 35 const testMessage = 'Test Message' 36 const { queryByText , getByLabelText , getByText } = render ( 37 < HiddenMessage > { testMessage } </ HiddenMessage > , 38 ) 39 40 41 42 expect ( queryByText ( testMessage ) ) . toBeNull ( ) 43 44 45 fireEvent . click ( getByLabelText ( /show/i ) ) 46 47 48 49 expect ( getByText ( testMessage ) ) . toBeInTheDocument ( ) 50 } )

Practical Example

1 2 import React from 'react' 3 4 function Login ( ) { 5 const [ state , setState ] = React . useReducer ( ( s , a ) => ( { ... s , ... a } ) , { 6 resolved : false , 7 loading : false , 8 error : null , 9 } ) 10 11 function handleSubmit ( event ) { 12 event . preventDefault ( ) 13 const { usernameInput , passwordInput } = event . target . elements 14 15 setState ( { loading : true , resolved : false , error : null } ) 16 17 window 18 . fetch ( '/api/login' , { 19 method : 'POST' , 20 headers : { 'Content-Type' : 'application/json' } , 21 body : JSON . stringify ( { 22 username : usernameInput . value , 23 password : passwordInput . value , 24 } ) , 25 } ) 26 . then ( r => r . json ( ) ) 27 . then ( 28 user => { 29 setState ( { loading : false , resolved : true , error : null } ) 30 window . localStorage . setItem ( 'token' , user . token ) 31 } , 32 error => { 33 setState ( { loading : false , resolved : false , error : error . message } ) 34 } , 35 ) 36 } 37 38 return ( 39 < div > 40 < form onSubmit = { handleSubmit } > 41 < div > 42 < label htmlFor = " usernameInput " > Username </ label > 43 < input id = " usernameInput " /> 44 </ div > 45 < div > 46 < label htmlFor = " passwordInput " > Password </ label > 47 < input id = " passwordInput " type = " password " /> 48 </ div > 49 < button type = " submit " > Submit { state . loading ? '...' : null } </ button > 50 </ form > 51 { state . error ? < div role = " alert " > { state . error . message } </ div > : null } 52 { state . resolved ? ( 53 < div role = " alert " > Congrats ! You're signed in ! </ div > 54 ) : null } 55 </ div > 56 ) 57 } 58 59 export default Login 60 61 62 63 64 import '@testing-library/jest-dom/extend-expect' 65 import React from 'react' 66 import { render , fireEvent } from '@testing-library/react' 67 import Login from '../login' 68 69 test ( 'allows the user to login successfully' , async ( ) => { 70 71 const fakeUserResponse = { token : 'fake_user_token' } 72 jest . spyOn ( window , 'fetch' ) . mockImplementationOnce ( ( ) => { 73 return Promise . resolve ( { 74 json : ( ) => Promise . resolve ( fakeUserResponse ) , 75 } ) 76 } ) 77 78 const { getByLabelText , getByText , findByRole } = render ( < Login /> ) 79 80 81 fireEvent . change ( getByLabelText ( /username/i ) , { target : { value : 'chuck' } } ) 82 fireEvent . change ( getByLabelText ( /password/i ) , { target : { value : 'norris' } } ) 83 84 fireEvent . click ( getByText ( /submit/i ) ) 85 86 87 88 const alert = await findByRole ( 'alert' ) 89 90 91 92 93 expect ( alert ) . toHaveTextContent ( /congrats/i ) 94 expect ( window . localStorage . getItem ( 'token' ) ) . toEqual ( fakeUserResponse . token ) 95 } )

The most important takeaway from this example is:

The test is written in such a way that resembles how the user is using your application.

Let's explore this further...

Let's say we have a GreetingFetcher component that fetches a greeting for a user. It might render some HTML like this:

1 < div > 2 < label for = " name-input " > Name </ label > 3 < input id = " name-input " /> 4 < button > Load Greeting </ button > 5 < div data-testid = " greeting-text " /> 6 </ div >

So the functionality is: Set the name, click the "Load Greeting" button, and a server request is made to load greeting text with that name.

In your test you'll need to find the <input /> so you can set its value to something. Conventional wisdom suggests you could use the id property in a CSS selector: #name-input . But is that what the user does to find that input? Definitely not! They look at the screen and find the input with the label "Name" and fill that in. So that's what our test is doing with getByLabelText . It gets the form control based on its label.

Often in tests using enzyme, to find the "Load Greeting" button you might use a CSS selector or even find by component displayName or the component constructor. But when the user wants to load the greeting, they don't care about those implementation details, instead they're going to find and click the button that says "Load Greeting." And that's exactly what our test is doing with the getByText helper!

In addition, the wait resembles exactly what the users does. They wait for the greeting text to appear, however long that takes. In our tests we're mocking that out so it happens basically instantly, but our test doesn't actually care how long it takes. We don't have to use a setTimeout in our test or anything. We simply say: "Hey, wait until the greeting-text node appears." (Note, in this case it's using a data-testid attribute which is an escape hatch for situations where it doesn't make sense to find an element by any other mechanism. A data-testid is definitely better then alternatives.

High-level Overview API

Originally, the library only provided queryByTestId as a utility as suggested in my blog post "Making your UI tests resilient to change". But thanks to feedback on that blog post from Bergé Greg as well as inspiration from a fantastic (and short!) talk by Jamie White, I added several more and now I'm even happier with this solution.

You can read more about the library and its APIs in the official docs. Here's a high-level overview of what this library gives you:

Simulate : a re-export from the Simulate utility from the react-dom/test-utils Simulate object.

: a re-export from the utility from the object. wait : allows you to wait for a non-deterministic period of time in your tests. Normally you should mock out API requests or animations, but even if you're dealing with immediately resolved promises, you'll need your tests to wait for the next tick of the event loop and wait is really good for that. (Big shout out to Łukasz Gozda Gandecki who introduced this as a replacement for the (now deprecated) flushPromises API).

: allows you to wait for a non-deterministic period of time in your tests. Normally you should mock out API requests or animations, but even if you're dealing with immediately resolved promises, you'll need your tests to wait for the next tick of the event loop and is really good for that. (Big shout out to Łukasz Gozda Gandecki who introduced this as a replacement for the (now deprecated) API). render : This is the meat of the library. It's fairly simple. It creates a div with document.createElement , then uses ReactDOM.render to render to that div .

The render function returns the following objects and utilities:

container : The div your component was rendered to

: The your component was rendered to unmount : A simple wrapper over ReactDOM.unmountComponentAtNode to unmount your component (to facilitate easier testing of componentWillUnmount for example).

: A simple wrapper over to unmount your component (to facilitate easier testing of for example). getByLabelText : Get a form control associated to a label

: Get a form control associated to a label getByPlaceholderText : Placeholders aren't proper alternatives to labels, but if this makes more sense for your use case it's available.

: Placeholders aren't proper alternatives to labels, but if this makes more sense for your use case it's available. getByText : Get any element by its text content.

: Get any element by its text content. getByAltText : Get an element (like an <img ) by it's alt attribute value.

: Get an element (like an ) by it's attribute value. getByTestId : Get an element by its data-testid attribute.

Each of those get* utilities will throw a useful error message if no element can be found. There's also an associated query* API for each which will return null instead of throwing an error which can be useful for asserting that an element is not in the DOM.

Also, for these get* utilities, to find a matching element, you can pass:

a case-insensitive substring: lo world matches Hello World

matches a regex: /^Hello World$/ matches Hello World

matches a function that accepts the text and the element: (text, el) => el.tagName === 'SPAN' && text.startsWith('Hello') would match a span that has content that starts with Hello

Custom Jest Matchers

Thanks to Anto Aravinth Belgin Rayen, we have some handy custom Jest matchers as well:

toBeInTheDOM : Assert whether an element present in the DOM or not.

: Assert whether an element present in the DOM or not. toHaveTextContent : Check whether the given element has a text content or not.

Note: now these have been extracted to jest-dom which is maintained by Ernesto García

Conclusion