dom-testing-library

octopus

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] [![All Contributors](https://img.shields.io/badge/all_contributors-51-orange.svg?style=flat-square)](#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]
TestingJavaScript.com Learn the smart, efficient way to test any JavaScript application.
## 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
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
Ryan Castner](http://audiolion.github.io)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [Daniel Sandiego
Daniel Sandiego](https://www.dnlsandiego.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [PaweΕ‚ MikoΕ‚ajczyk
PaweΕ‚ MikoΕ‚ajczyk](https://github.com/Miklet)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [Alejandro ÑÑñez Ortiz
Alejandro ÑÑñez Ortiz](http://co.linkedin.com/in/alejandronanez/)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [Matt Parrish
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
Justin Hall](https://github.com/wKovacs64)
[πŸ“¦](#platform-wKovacs64 "Packaging/porting to new platform") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [Anto Aravinth
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
Jonah Moses](https://github.com/JonahMoses)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [Łukasz Gandecki
Ł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
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
Jesse Day](https://github.com/jday3)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [Ernesto GarcΓ­a
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
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
Alex Cook](https://github.com/alecook)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [πŸ’‘](#example-alecook "Examples") | [Daniel Cook
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
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
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
Alex Krolick](https://alexkrolick.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [Maddi Joyce
Maddi Joyce](http://www.maddijoyce.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") | [Peter Kamps
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
Jonathan Stoye](http://jonathanstoye.de)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [Sanghyeon Lee
Sanghyeon Lee](https://github.com/yongdamsh)
[πŸ’‘](#example-yongdamsh "Examples") | [Justice Mba
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
Wayne Crouch](https://github.com/wgcrouch)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [Ben Elliott
Ben Elliott](http://benjaminelliott.co.uk)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [Ruben Costa
Ruben Costa](http://nuances.co)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") | [Robert Smith
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
dadamssg](https://github.com/dadamssg)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [Neil Kistner
Neil Kistner](https://neilkistner.com/)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [Ben Chauvette
Ben Chauvette](http://bdchauvette.net/)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [Jeff Baumgardt
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
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
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
Tamas Fodor](https://github.com/ruffle1986)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=ruffle1986 "Documentation") | | [Benjamin Eckardt
Benjamin Eckardt](https://github.com/BenjaminEckardt)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=BenjaminEckardt "Code") | [Ryan Campbell
Ryan Campbell](https://github.com/campbellr)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=campbellr "Documentation") | [Taylor Briggs
Taylor Briggs](https://taylor-briggs.com)
[⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=TaylorBriggs "Tests") | [John Gozde
John Gozde](https://github.com/jgoz)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=jgoz "Code") | [C. T. Lin
C. T. Lin](https://github.com/chentsulin)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=chentsulin "Documentation") | [Terrence Wong
Terrence Wong](http://terrencewwong.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=terrencewwong "Code") | [Soo Jae Hwang
Soo Jae Hwang](https://www.ossfinder.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=misoguy "Code") | | [Royston Shufflebotham
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
Vadim Brodsky](http://www.vadimbrodsky.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=VadimBrodsky "Code") | [Eunjae Lee
Eunjae Lee](https://twitter.com/eunjae_lee)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=eunjae-lee "Code") | [David Peter
David Peter](http://davidpeter.me)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=sarenji "Code") | [Shy Alter
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
Łukasz Makuch](https://lukaszmakuch.pl)
[πŸ“¦](#platform-lukaszmakuch "Packaging/porting to new platform") | [Tyler Haas
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
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
Tulsi Sapkota](https://github.com/Tolsee)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=Tolsee "Code") | [Tiago Nunes
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