Simple and complete DOM testing utilities that encourage good testing practices.
[**Read the docs**](https://testing-library.com/) | [Edit the docs](https://github.com/alexkrolick/testing-library-docs)
[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
[![version][version-badge]][package]
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]
[](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]
[![Watch on GitHub][github-watch-badge]][github-watch]
[![Star on GitHub][github-star-badge]][github-star]
[![Tweet][twitter-badge]][twitter]
## Table of Contents
- [The Problem](#the-problem)
- [This Solution](#this-solution)
- [Installation](#installation)
- [Implementations](#implementations)
- [Other Solutions](#other-solutions)
- [Guiding Principles](#guiding-principles)
- [Contributors](#contributors)
- [LICENSE](#license)
## The Problem
You want to write maintainable tests for your Web UI. 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 `dom-testing-library` is a very light-weight solution for testing DOM nodes
(whether simulated with [`JSDOM`](https://github.com/jsdom/jsdom) as provided by
default with [jest][] or in the browser). The main utilities it provides involve
querying the DOM for nodes in a way that's similar to how the user finds
elements on the page. In this way, the library helps ensure your tests give you
confidence in your UI code. The `dom-testing-library`'s primary guiding
principle is:
> [The more your tests resemble the way your software is used, the more confidence they can give you.][guiding-principle]
## Installation
This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's `devDependencies`:
```
npm install --save-dev dom-testing-library
```
> [**Docs**](https://testing-library.com/docs/install)
## Implementations
This library was not built to be used on its own. The original implementation
of these utilities was in the `react-testing-library`.
Implementations include:
- [`react-testing-library`](https://github.com/kentcdodds/react-testing-library)
- [`pptr-testing-library`](https://github.com/patrickhulce/pptr-testing-library)
- [`cypress-testing-library`](https://github.com/kentcdodds/cypress-testing-library)
- [`vue-testing-library`](https://github.com/dfcook/vue-testing-library)
- [`snabbdom-testing-library`](https://github.com/lukaszmakuch/snabbdom-testing-library)
## Other Solutions
- [browser-monkey](https://github.com/featurist/browser-monkey)
## Guiding Principles
> [The more your tests resemble the way your software is used, the more confidence they can give you.][guiding-principle]
We try to only expose methods and utilities that encourage you to write tests
that closely resemble how your web pages are used.
Utilities are included in this project based on the following guiding
principles:
1. If it relates to rendering components, it deals with DOM nodes rather than
component instances, nor should it encourage dealing with component
instances.
2. It should be generally useful for testing the application components in the
way the user would use it. We _are_ making some trade-offs here because
we're using a computer and often a simulated browser environment, but in
general, utilities should encourage tests that use the components the way
they're intended to be used.
3. Utility implementations and APIs should be simple and flexible.
At the end of the day, what we want is for this library to be pretty
light-weight, simple, and understandable.
## Contributors
Thanks goes to these people ([emoji key][emojis]):
| [ Kent C. Dodds](https://kentcdodds.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Documentation") [π](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Tests") | [ Ryan Castner](http://audiolion.github.io) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [ Daniel Sandiego](https://www.dnlsandiego.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [ PaweΕ MikoΕajczyk](https://github.com/Miklet) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [ Alejandro ΓÑñez Ortiz](http://co.linkedin.com/in/alejandronanez/) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [ Matt Parrish](https://github.com/pbomb) [π](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Apbomb "Bug reports") [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Documentation") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Tests") | [ Justin Hall](https://github.com/wKovacs64) [π¦](#platform-wKovacs64 "Packaging/porting to new platform") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [ Anto Aravinth](https://github.com/antoaravinth) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [ Jonah Moses](https://github.com/JonahMoses) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [ Εukasz Gandecki](http://team.thebrain.pro) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [ Ivan Babak](https://sompylasar.github.io) [π](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [π€](#ideas-sompylasar "Ideas, Planning, & Feedback") [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [ Jesse Day](https://github.com/jday3) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [ Ernesto GarcΓa](http://gnapse.github.io) [π¬](#question-gnapse "Answering Questions") [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") | [ Josef Maxx Blake](http://jomaxx.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Documentation") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Tests") |
| [ Alex Cook](https://github.com/alecook) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [π‘](#example-alecook "Examples") | [ Daniel Cook](https://github.com/dfcook) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [ Thomas Chia](https://github.com/thchia) [π](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [ Tim Deschryver](https://github.com/tdeschryver) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [ Alex Krolick](https://alexkrolick.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [ Maddi Joyce](http://www.maddijoyce.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") | [ Peter Kamps](https://github.com/npeterkamps) [π](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Anpeterkamps "Bug reports") [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Code") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Tests") |
| [ Jonathan Stoye](http://jonathanstoye.de) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [ Sanghyeon Lee](https://github.com/yongdamsh) [π‘](#example-yongdamsh "Examples") | [ Justice Mba ](https://github.com/Dajust) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Documentation") [π€](#ideas-Dajust "Ideas, Planning, & Feedback") | [ Wayne Crouch](https://github.com/wgcrouch) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [ Ben Elliott](http://benjaminelliott.co.uk) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [ Ruben Costa](http://nuances.co) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") | [ Robert Smith](http://rbrtsmith.com/) [π](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Arbrtsmith "Bug reports") [π€](#ideas-rbrtsmith "Ideas, Planning, & Feedback") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=rbrtsmith "Documentation") |
| [ dadamssg](https://github.com/dadamssg) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [ Neil Kistner](https://neilkistner.com/) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [ Ben Chauvette](http://bdchauvette.net/) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [ Jeff Baumgardt](https://github.com/JeffBaumgardt) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Documentation") | [ Matan Kushner](http://matchai.me) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Documentation") [π€](#ideas-matchai "Ideas, Planning, & Feedback") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Tests") | [ Alex Wendte](http://www.wendtedesigns.com/) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Documentation") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Tests") | [ Tamas Fodor](https://github.com/ruffle1986) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=ruffle1986 "Documentation") |
| [ Benjamin Eckardt](https://github.com/BenjaminEckardt) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=BenjaminEckardt "Code") | [ Ryan Campbell](https://github.com/campbellr) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=campbellr "Documentation") | [ Taylor Briggs](https://taylor-briggs.com) [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=TaylorBriggs "Tests") | [ John Gozde](https://github.com/jgoz) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=jgoz "Code") | [ C. T. Lin](https://github.com/chentsulin) [π](https://github.com/kentcdodds/dom-testing-library/commits?author=chentsulin "Documentation") | [ Terrence Wong](http://terrencewwong.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=terrencewwong "Code") | [ Soo Jae Hwang](https://www.ossfinder.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=misoguy "Code") |
| [ Royston Shufflebotham](https://github.com/RoystonS) [π](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3ARoystonS "Bug reports") [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Documentation") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Tests") | [ Vadim Brodsky](http://www.vadimbrodsky.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=VadimBrodsky "Code") | [ Eunjae Lee](https://twitter.com/eunjae_lee) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=eunjae-lee "Code") | [ David Peter](http://davidpeter.me) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=sarenji "Code") | [ Shy Alter](https://twitter.com/@puemos) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=puemos "Code") [π](https://github.com/kentcdodds/dom-testing-library/commits?author=puemos "Documentation") | [ Εukasz Makuch](https://lukaszmakuch.pl) [π¦](#platform-lukaszmakuch "Packaging/porting to new platform") | [ Tyler Haas](https://github.com/tylerthehaas) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=tylerthehaas "Code") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=tylerthehaas "Tests") |
| [ Vesa Laakso](http://vesalaakso.com) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Code") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Tests") | [ Tulsi Sapkota](https://github.com/Tolsee) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=Tolsee "Code") | [ Tiago Nunes](https://github.com/tnunes) [π»](https://github.com/kentcdodds/dom-testing-library/commits?author=tnunes "Code") [β οΈ](https://github.com/kentcdodds/dom-testing-library/commits?author=tnunes "Tests") |
This project follows the [all-contributors][all-contributors] specification.
Contributions of any kind welcome!
## LICENSE
MIT
[npm]: https://www.npmjs.com/
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/travis/kentcdodds/dom-testing-library.svg?style=flat-square
[build]: https://travis-ci.org/kentcdodds/dom-testing-library
[coverage-badge]: https://img.shields.io/codecov/c/github/kentcdodds/dom-testing-library.svg?style=flat-square
[coverage]: https://codecov.io/github/kentcdodds/dom-testing-library
[version-badge]: https://img.shields.io/npm/v/dom-testing-library.svg?style=flat-square
[package]: https://www.npmjs.com/package/dom-testing-library
[downloads-badge]: https://img.shields.io/npm/dm/dom-testing-library.svg?style=flat-square
[npmtrends]: http://www.npmtrends.com/dom-testing-library
[license-badge]: https://img.shields.io/npm/l/dom-testing-library.svg?style=flat-square
[license]: https://github.com/kentcdodds/dom-testing-library/blob/master/LICENSE
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs]: http://makeapullrequest.com
[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://github.com/kentcdodds/dom-testing-library/blob/master/CODE_OF_CONDUCT.md
[github-watch-badge]: https://img.shields.io/github/watchers/kentcdodds/dom-testing-library.svg?style=social
[github-watch]: https://github.com/kentcdodds/dom-testing-library/watchers
[github-star-badge]: https://img.shields.io/github/stars/kentcdodds/dom-testing-library.svg?style=social
[github-star]: https://github.com/kentcdodds/dom-testing-library/stargazers
[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20dom-testing-library%20by%20%40kentcdodds%20https%3A%2F%2Fgithub.com%2Fkentcdodds%2Fdom-testing-library%20%F0%9F%91%8D
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/kentcdodds/dom-testing-library.svg?style=social
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
[all-contributors]: https://github.com/kentcdodds/all-contributors
[set-immediate]: https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
[guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106
[data-testid-blog-post]: https://blog.kentcdodds.com/making-your-ui-tests-resilient-to-change-d37a6ee37269
[jest]: https://facebook.github.io/jest