@@ -38,11 +38,11 @@ your team down.
38
38
39
39
The ` dom-testing-library ` is a very light-weight solution for testing DOM nodes
40
40
(whether simulated with [ ` JSDOM ` ] ( https://github.com/jsdom/jsdom ) as provided by
41
- default with [ jest] ( https://facebook.github.io/jest ) or in the browser). The
42
- main utilities it provides involve querying the DOM for nodes in a way that's
43
- similar to how the user finds elements on the page. In this way, the library
44
- helps ensure your tests give you confidence in your UI code. The
45
- ` dom-testing-library ` 's primary guiding principle is:
41
+ default with [ jest] [ ] or in the browser). The main utilities it provides involve
42
+ querying the DOM for nodes in a way that's similar to how the user finds
43
+ elements on the page. In this way, the library helps ensure your tests give you
44
+ confidence in your UI code. The ` dom-testing-library ` 's primary guiding
45
+ principle is:
46
46
47
47
> [ The more your tests resemble the way your software is used, the more confidence they can give you.] [ guiding-principle ]
48
48
@@ -80,11 +80,7 @@ when a real user uses it.
80
80
* [ ` waitForElement ` ] ( #waitforelement )
81
81
* [ ` fireEvent(node: HTMLElement, event: Event) ` ] ( #fireeventnode-htmlelement-event-event )
82
82
* [ Custom Jest Matchers] ( #custom-jest-matchers )
83
- * [ ` toBeInTheDOM ` ] ( #tobeinthedom )
84
- * [ ` toHaveTextContent ` ] ( #tohavetextcontent )
85
- * [ ` toHaveAttribute ` ] ( #tohaveattribute )
86
- * [ ` toHaveClass ` ] ( #tohaveclass )
87
- * [ Custom Jest Matchers - Typescript] ( #custom-jest-matchers---typescript )
83
+ * [ Using other assertion libraries] ( #using-other-assertion-libraries )
88
84
* [ ` TextMatch ` ] ( #textmatch )
89
85
* [ ` query ` APIs] ( #query-apis )
90
86
* [ Debugging] ( #debugging )
@@ -400,106 +396,37 @@ fireEvent.click(getElementByText('Submit'), rightClick)
400
396
401
397
## Custom Jest Matchers
402
398
403
- There are two simple API which extend the ` expect ` API of jest for making assertions easier .
404
-
405
- ### ` toBeInTheDOM `
406
-
407
- This allows you to assert whether an element present in the DOM or not .
399
+ When using [jest ][], we recommend that you import a set of custom matchers that
400
+ make it easier to check several aspects of the state of a DOM element . These are
401
+ provided by [jest -dom ](https :// github.com/gnapse/jest-dom), but are also
402
+ included for convenience to be imported from this library directly :
408
403
409
404
` ` ` javascript
410
- // add the custom expect matchers
411
405
import 'dom-testing-library/extend-expect'
412
406
407
+ // <span data-testid="greetings">Hello World</span>
408
+ expect(queryByText(container, 'greetings')).toBeInTheDOM()
409
+ expect(queryByText(container, 'greetings')).not.toHaveTextContent('Bye bye')
413
410
// ...
414
- // <span data-testid="count-value">2</span>
415
- expect(queryByTestId(container, 'count-value')).toBeInTheDOM()
416
- expect(queryByTestId(container, 'count-value1')).not.toBeInTheDOM()
417
- // ...
418
- ` ` `
419
411
420
412
> Note: when using ` toBeInTheDOM ` , make sure you use a query function
421
413
> (like ` queryByTestId ` ) rather than a get function (like ` getByTestId ` ).
422
414
> Otherwise the ` get *` function could throw an error before your assertion.
423
-
424
- ### ` toHaveTextContent `
425
-
426
- This API allows you to check whether the given element has a text content or not .
427
-
428
- ` ` ` javascript
429
- // add the custom expect matchers
430
- import 'dom-testing-library/extend-expect'
431
-
432
- // ...
433
- // <span data-testid="count-value">2</span>
434
- expect(getByTestId(container, 'count-value')).toHaveTextContent('2')
435
- expect(getByTestId(container, 'count-value')).not.toHaveTextContent('21')
436
- // ...
437
- ` ` `
438
-
439
- ### ` toHaveAttribute `
440
-
441
- This allows you to check wether the given element has an attribute or not . You
442
- can also optionally check that the attribute has a specific expected value .
443
-
444
- ` ` ` javascript
445
- // add the custom expect matchers
446
- import 'dom-testing-library/extend-expect'
447
-
448
- // ...
449
- // <button data-testid="ok-button" type="submit" disabled>
450
- // OK
451
- // </button>
452
- expect(getByTestId(container, 'ok-button')).toHaveAttribute('disabled')
453
- expect(getByTestId(container, 'ok-button')).toHaveAttribute('type', 'submit')
454
- expect(getByTestId(container, 'ok-button')).not.toHaveAttribute(
455
- 'type',
456
- 'button',
457
- )
458
- // ...
459
415
` ` `
460
416
461
- ### ` toHaveClass `
417
+ Check out [jest -dom ' s documentation](https://github.com/gnapse/jest-dom#readme)
418
+ for a full list of available matchers .
462
419
463
- This allows you to check wether the given element has certain classes within its
464
- ` class ` attribute .
420
+ ### Using other assertion libraries
465
421
466
- ` ` ` javascript
467
- // add the custom expect matchers
468
- import 'dom-testing-library/extend-expect'
422
+ If you ' re not using jest, you may be able to find a similar set of custo m
423
+ assertions for your library of choice . Here ' s a list of alternatives to jest-do m
424
+ for other popular assertion libraries :
469
425
470
- // ...
471
- // <button data-testid="delete-button" class="btn extra btn-danger">
472
- // Delete item
473
- // </button>
474
- expect(getByTestId(container, 'delete-button')).toHaveClass('extra')
475
- expect(getByTestId(container, 'delete-button')).toHaveClass('btn-danger btn')
476
- expect(getByTestId(container, 'delete-button')).not.toHaveClass('btn-link')
477
- // ...
478
- ` ` `
479
-
480
- ### Custom Jest Matchers - Typescript
426
+ * [chai -dom ](https :// github.com/nathanboktae/chai-dom)
481
427
482
- When you use custom Jest Matchers with Typescript , you will need to extend the
483
- type signature of ` jest.Matchers<void> ` , then cast the result of ` expect `
484
- accordingly . Here ' s a handy usage example:
485
-
486
- ` ` ` typescript
487
- import {getByTestId} from 'dom-testing-library'
488
- // this adds custom expect matchers
489
- import 'dom-testing-library/extend-expect'
490
- interface ExtendedMatchers extends jest.Matchers<void> {
491
- toHaveTextContent: (htmlElement: string) => object
492
- toBeInTheDOM: () => void
493
- }
494
- test('renders the tooltip as expected', async () => {
495
- // however you render it:
496
- // render( ` <div ><span data -testid = " greeting" > hello world < / span></ div > ` )
497
- ;(expect(
498
- container,
499
- getByTestId('greeting'),
500
- ) as ExtendedMatchers).toHaveTextContent('hello world')
501
- })
502
- ` ` `
428
+ If you ' re aware of some other alternatives, please [make a pull request][prs]
429
+ and add it here !
503
430
504
431
## ` TextMatch `
505
432
768
695
[set -immediate ]: https :// developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
769
696
[guiding -principle ]: https :// twitter.com/kentcdodds/status/977018512689455106
770
697
[data -testid -blog -post ]: https :// blog.kentcdodds.com/making-your-ui-tests-resilient-to-change-d37a6ee37269
698
+ [jest ]: https :// facebook.github.io/jest
0 commit comments