1616[ ![ downloads] [ downloads-badge ]] [ npmtrends ]
1717[ ![ MIT License] [ license-badge ]] [ license ]
1818
19- [ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-27 -orange.svg?style=flat-square )] ( #contributors )
19+ [ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-28 -orange.svg?style=flat-square )] ( #contributors )
2020[ ![ PRs Welcome] [ prs-badge ]] [ prs ]
2121[ ![ Code of Conduct] [ coc-badge ]] [ coc ]
2222[ ![ Join the community on Spectrum] [ spectrum-badge ]] [ spectrum ]
@@ -171,6 +171,8 @@ The containing DOM node of your rendered React Element (rendered using
171171This method is a shortcut for ` console.log(prettyDOM(container)) ` .
172172
173173``` javascript
174+ import {render } from ' react-testing-library'
175+
174176const HelloWorld = () => < h1> Hello World< / h1>
175177const {debug } = render (< HelloWorld / > )
176178debug ()
@@ -190,6 +192,8 @@ prefer to update the props of a rendered component in your test, this function
190192can be used to update props of the rendered component.
191193
192194``` javascript
195+ import {render } from ' react-testing-library'
196+
193197const {rerender } = render (< NumberDisplay number= {1 } / > )
194198
195199// re-render the same component with different props
@@ -209,6 +213,8 @@ that you don't leave event handlers hanging around causing memory leaks).
209213> ` ReactDOM.unmountComponentAtNode `
210214
211215``` javascript
216+ import {render } from ' react-testing-library'
217+
212218const {container , unmount } = render (< Login / > )
213219unmount ()
214220// your component has been unmounted and now: container.innerHTML === ''
@@ -220,6 +226,9 @@ This will search for the label that matches the given [`TextMatch`](#textmatch),
220226then find the element associated with that label.
221227
222228``` javascript
229+ import {render } from ' react-testing-library'
230+
231+ const {getByLabelText } = render (< Login / > )
223232const inputNode = getByLabelText (' Username' )
224233
225234// this would find the input node for the following DOM structures:
@@ -255,7 +264,9 @@ This will search for all elements with a placeholder attribute and find one
255264that matches the given [ ` TextMatch ` ] ( #textmatch ) .
256265
257266``` javascript
258- // <input placeholder="Username" />
267+ import {render } from ' react-testing-library'
268+
269+ const {getByPlaceholderText } = render (< input placeholder= " Username" / > )
259270const inputNode = getByPlaceholderText (' Username' )
260271```
261272
@@ -268,7 +279,9 @@ This will search for all elements that have a text node with `textContent`
268279matching the given [ ` TextMatch ` ] ( #textmatch ) .
269280
270281``` javascript
271- // <a href="/about">About ℹ️</a>
282+ import {render } from ' react-testing-library'
283+
284+ const {getByText } = render (< a href= " /about" > About ℹ️< / a> )
272285const aboutAnchorNode = getByText (' about' )
273286```
274287
@@ -282,7 +295,11 @@ and [`<area>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area)
282295(intentionally excluding [ ` <applet> ` ] ( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/applet ) as it's deprecated).
283296
284297``` javascript
285- // <img alt="Incredibles 2 Poster" src="/incredibles-2.png" />
298+ import {render } from ' react-testing-library'
299+
300+ const {getByAltText } = render (
301+ < img alt= " Incredibles 2 Poster" src= " /incredibles-2.png" / > ,
302+ )
286303const incrediblesPosterImg = getByAltText (/ incredibles. * poster$ / i )
287304```
288305
@@ -292,7 +309,9 @@ A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
292309also accepts a [ ` TextMatch ` ] ( #textmatch ) ).
293310
294311``` javascript
295- // <input data-testid="username-input" />
312+ import {render } from ' react-testing-library'
313+
314+ const {getByTestId } = render (< input data- testid= " username-input" / > )
296315const usernameInputElement = getByTestId (' username-input' )
297316```
298317
@@ -310,18 +329,22 @@ Render into `document.body`. Should be used with [cleanup](#cleanup).
310329` renderIntoDocument ` will return the same object as [ render] ( #render )
311330
312331``` javascript
313- renderIntoDocument (< div> )
332+ import {renderIntoDocument } from ' react-testing-library'
333+
334+ renderIntoDocument (< div / > )
314335```
315336
316337### ` cleanup `
317338
318339Unmounts React trees that were mounted with [ renderIntoDocument] ( #renderintodocument ) .
319340
320341``` javascript
342+ import {cleanup , renderIntoDocument } from ' react-testing-library'
343+
321344afterEach (cleanup)
322345
323346test (' renders into document' , () => {
324- renderIntoDocument (< div> )
347+ renderIntoDocument (< div / > )
325348 // ...
326349})
327350```
@@ -366,12 +389,16 @@ around the
366389Here ' s a simple example:
367390
368391` ` ` javascript
369- // ...
370- // wait until the callback does not throw an error. In this case, that means
371- // it'll wait until we can get a form control with a label that matches "username"
372- await wait(() => getByLabelText('username'))
373- getByLabelText('username').value = 'chucknorris'
374- // ...
392+ import {render, wait} from 'react-testing-library'
393+
394+ test('waiting for an expectation to pass before proceeding', async () => {
395+ const {getByLabelText} = render(<Login />)
396+
397+ // wait until the callback does not throw an error. In this case, that means
398+ // it'll wait until we can get a form control with a label that matches "username"
399+ await wait(() => getByLabelText('username'))
400+ getByLabelText('username').value = 'chucknorris'
401+ })
375402` ` `
376403
377404This can be useful if you have a unit test that mocks API calls and you need
@@ -396,7 +423,13 @@ intervals.
396423See [dom - testing - library #waitForElement ](https :// github.com/kentcdodds/dom-testing-library#waitforelement)
397424
398425` ` ` js
399- await waitForElement(() => getByText('Search'))
426+ import {render, waitForElement} from 'react-testing-library'
427+
428+ test('waiting for an element', async () => {
429+ const {getByText} = render(<SearchForm />)
430+
431+ await waitForElement(() => getByText('Search'))
432+ })
400433` ` `
401434
402435<details >
@@ -405,6 +438,8 @@ await waitForElement(() => getByText('Search'))
405438 < / summary >
406439
407440` ` ` diff
441+ import {render, Simulate, waitForElement} from 'react-testing-library'
442+
408443test('should submit form when valid', async () => {
409444 const mockSubmit = jest.fn()
410445 const {
@@ -445,12 +480,7 @@ instead of Synthetic Events. This aligns better with
445480> [facebook / react #2043 ](https :// github.com/facebook/react/issues/2043)
446481
447482` ` ` javascript
448- import {
449- renderIntoDocument,
450- cleanup,
451- render,
452- fireEvent,
453- } from 'react-testing-library'
483+ import {renderIntoDocument, cleanup, fireEvent} from 'react-testing-library'
454484
455485// don't forget to clean up the document.body
456486afterEach(cleanup)
@@ -478,6 +508,10 @@ Convenience methods for firing DOM events. Check out
478508for a full list as well as default ` eventProperties ` .
479509
480510` ` ` javascript
511+ import {renderIntoDocument, fireEvent} from 'react-testing-library'
512+
513+ const {getElementByText} = renderIntoDocument(<Form />)
514+
481515// similar to the above example
482516// click will bubble for React to see it
483517const rightClick = {button: 2}
@@ -503,6 +537,8 @@ This function is usually used alongside `console.log` to temporarily print out
503537DOM trees during tests for debugging purposes:
504538
505539```javascript
540+ import {render , prettyDOM } from ' react-testing-library'
541+
506542const HelloWorld = () => <h1 >Hello World < / h1 >
507543const {container } = render (<HelloWorld />)
508544console .log (prettyDOM (container ))
@@ -521,9 +557,9 @@ See [dom-testing-library#textmatch][dom-testing-lib-textmatch] for options.
521557Examples:
522558
523559` ` ` javascript
524- // <div>
525- // Hello World
526- // < /div>
560+ import { container , getByText } from ' react-testing-library '
561+
562+ const { container } = render (< div > Hello World < / div > )
527563
528564// WILL find the div:
529565
@@ -560,6 +596,9 @@ make an assertion that an element is _not_ present in the DOM, then you can use
560596the ` query ` API instead:
561597
562598` ` ` javascript
599+ import {render } from ' react-testing-library'
600+
601+ const {queryByText } = render (<Form />)
563602const submitButton = queryByText (' submit' )
564603expect (submitButton ).toBeNull () // it doesn't exist
565604` ` `
@@ -569,6 +608,9 @@ expect(submitButton).toBeNull() // it doesn't exist
569608Each of the ` query ` APIs have a corresponsing ` queryAll ` version that always returns an Array of matching nodes. ` getAll ` is the same but throws when the array has a length of 0.
570609
571610` ` ` javascript
611+ import {render } from ' react-testing-library'
612+
613+ const {queryByText } = render (<Forms />)
572614const submitButtons = queryAllByText (' submit' )
573615expect (submitButtons ).toHaveLength (3 ) // expect 3 elements
574616expect (submitButtons [0 ]).toBeInTheDOM ()
@@ -855,14 +897,12 @@ light-weight, simple, and understandable.
855897Thanks goes to these people ([emoji key][emojis]):
856898
857899<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
858-
859900<!-- prettier-ignore -->
860901| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
861902| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
862903| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Tests") |
863904| [<img src="https://avatars1.githubusercontent.com/u/29602306?v=4" width="100px;"/><br /><sub><b>Michal Baranowski</b></sub>](https://twitter.com/baranovskim)<br />[📝](#blog-mbaranovski "Blogposts") [✅](#tutorial-mbaranovski "Tutorials") | [<img src="https://avatars3.githubusercontent.com/u/13985684?v=4" width="100px;"/><br /><sub><b>Arthur Puthin</b></sub>](https://github.com/aputhin)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=aputhin "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/21194045?v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/20430611?v=4" width="100px;"/><br /><sub><b>Thiago Galvani</b></sub>](http://ilegra.com/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=thiagopaiva99 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/19828824?v=4" width="100px;"/><br /><sub><b>Christian</b></sub>](http://Chriswcs.github.io)<br />[⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=ChrisWcs "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💬](#question-alexkrolick "Answering Questions") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=alexkrolick "Documentation") [💡](#example-alexkrolick "Examples") [🤔](#ideas-alexkrolick "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/1239401?v=4" width="100px;"/><br /><sub><b>Johann Hubert Sonntagbauer</b></sub>](https://github.com/johann-sonntagbauer)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Tests") |
864- | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars2.githubusercontent.com/u/10080111?v=4" width="100px;"/><br /><sub><b>Ryan Vice</b></sub>](http://www.vicesoftware.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=RyanAtViceSoftware "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/7942604?v=4" width="100px;"/><br /><sub><b>Ian Wilson</b></sub>](https://ianwilson.io)<br />[📝](#blog-iwilsonq "Blogposts") [✅](#tutorial-iwilsonq "Tutorials") | [<img src="https://avatars2.githubusercontent.com/u/1635491?v=4" width="100px;"/><br /><sub><b>Daniel</b></sub>](https://github.com/InExtremaRes)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AInExtremaRes "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=InExtremaRes "Code") | [<img src="https://avatars0.githubusercontent.com/u/767959?v=4" width="100px;"/><br /><sub><b>Giorgio Polvara</b></sub>](https://twitter.com/Gpx)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AGpx "Bug reports") [🤔](#ideas-Gpx "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jgoz "Code") |
865-
905+ | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars2.githubusercontent.com/u/10080111?v=4" width="100px;"/><br /><sub><b>Ryan Vice</b></sub>](http://www.vicesoftware.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=RyanAtViceSoftware "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/7942604?v=4" width="100px;"/><br /><sub><b>Ian Wilson</b></sub>](https://ianwilson.io)<br />[📝](#blog-iwilsonq "Blogposts") [✅](#tutorial-iwilsonq "Tutorials") | [<img src="https://avatars2.githubusercontent.com/u/1635491?v=4" width="100px;"/><br /><sub><b>Daniel</b></sub>](https://github.com/InExtremaRes)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AInExtremaRes "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=InExtremaRes "Code") | [<img src="https://avatars0.githubusercontent.com/u/767959?v=4" width="100px;"/><br /><sub><b>Giorgio Polvara</b></sub>](https://twitter.com/Gpx)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AGpx "Bug reports") [🤔](#ideas-Gpx "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jgoz "Code") | [<img src="https://avatars0.githubusercontent.com/u/8203211?v=4" width="100px;"/><br /><sub><b>Sam Horton</b></sub>](https://twitter.com/SavePointSam)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=SavePointSam "Documentation") [💡](#example-SavePointSam "Examples") [🤔](#ideas-SavePointSam "Ideas, Planning, & Feedback") |
866906<!-- ALL-CONTRIBUTORS-LIST:END -->
867907
868908This project follows the [all-contributors][all-contributors] specification.
0 commit comments