React Testing Library
React Testing Library
builds on top ofDOM Testing Library
by addingAPIs for working with React components.
Installation
To get started withReact Testing Library
, you'll need to install it togetherwith its peerDependency@testing-library/dom
:
- npm
- Yarn
npminstall --save-dev @testing-library/react @testing-library/dom
yarnadd --dev @testing-library/react @testing-library/dom
With TypeScript
To get full type coverage, you need to install the types forreact
andreact-dom
as well:
- npm
- Yarn
npminstall --save-dev @testing-library/react @testing-library/dom @types/react @types/react-dom
yarnadd --dev @testing-library/react @testing-library/dom @types/react @types/react-dom
The problem
You want to write maintainable tests for your React components. As a part ofthis goal, you want your tests to avoid including implementation details of yourcomponents and rather focus on making your tests give you the confidence forwhich they are intended. As part of this, you want your testbase to bemaintainable in the long run so refactors of your components (changes toimplementation but not functionality) don't break your tests and slow you andyour team down.
This solution
TheReact Testing Library
is a very light-weight solution for testing Reactcomponents. It provides light utility functions on top ofreact-dom
andreact-dom/test-utils
, in a way that encourages better testing practices. Itsprimary guiding principle is:
The more your tests resemble the way your software is used, the more confidence they can give you.
So rather than dealing with instances of rendered React components, your testswill work with actual DOM nodes. The utilities this library provides facilitatequerying the DOM in the same way the user would. Finding form elements by theirlabel text (just like a user would), finding links and buttons from their text(like a user would). It also exposes a recommended way to find elements by adata-testid
as an "escape hatch" for elements where the text content and labeldo not make sense or is not practical.
This library encourages your applications to be more accessible and allows youto get your tests closer to using your components the way a user will, whichallows your tests to give you more confidence that your application will workwhen a real user uses it.
This library is a replacement forEnzyme. While youcan follow these guidelines using Enzyme itself, enforcing this is harderbecause of all the extra utilities that Enzyme provides (utilities whichfacilitate testing implementation details). Read more about this inthe FAQ.
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. SeeUsing Without Jest)
NOTE: This library is built on top of
DOM Testing Library
which is where most ofthe logic behind the queries is.
Tutorials
Have a look at the "What is React Testing library?" video below for anintroduction to the library.

Also, don't miss thistutorial for React Testing Library.