Skip to content

Commit 6d9c368

Browse files
author
Kent C. Dodds
committed
feat: extract most of the logic to dom-testing-library
BREAKING CHANGE: This removes some deprecated APIs, but should be a safe upgrade for you if you're not using `flushPromises` ( use `wait` instead)
1 parent 7267acd commit 6d9c368

14 files changed

+30
-645
lines changed

.github/ISSUE_TEMPLATE.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<!--
2+
HI! PLEASE STOP TO READ THIS!! If you're issue is regarding one of the query
3+
APIs (`getByText`, `getByLabelText`, etc), then please file it on the
4+
`dom-testing-library` repository instead. If you file it here it will be closed.
5+
Thanks :)
6+
27
Thanks for your interest in the project. I appreciate bugs filed and PRs submitted!
38
Please make sure that you are familiar with and follow the Code of Conduct for
49
this project (found in the CODE_OF_CONDUCT.md file).

README.md

+9-117
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ facilitate testing implementation details). Read more about this in
6868
2. Specific to a testing framework (though we recommend Jest as our
6969
preference, the library works with any framework)
7070

71+
> NOTE: This library is built on top of
72+
> [`dom-testing-library`](https://github.com/kentcdodds/dom-testing-library)
73+
> which is where most of the logic behind the queries is.
74+
7175
## Table of Contents
7276

7377
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
@@ -79,17 +83,10 @@ facilitate testing implementation details). Read more about this in
7983
* [`render`](#render)
8084
* [`Simulate`](#simulate)
8185
* [`wait`](#wait)
82-
* [Custom Jest Matchers](#custom-jest-matchers)
83-
* [`toBeInTheDOM`](#tobeinthedom)
84-
* [`toHaveTextContent`](#tohavetextcontent)
85-
* [`toHaveAttribute`](#tohaveattribute)
86-
* [Custom Jest Matchers - Typescript](#custom-jest-matchers---typescript)
8786
* [`TextMatch`](#textmatch)
8887
* [`query` APIs](#query-apis)
8988
* [Examples](#examples)
9089
* [FAQ](#faq)
91-
* [Deprecated APIs](#deprecated-apis)
92-
* [`flushPromises`](#flushpromises)
9390
* [Other Solutions](#other-solutions)
9491
* [Guiding Principles](#guiding-principles)
9592
* [Contributors](#contributors)
@@ -108,13 +105,17 @@ npm install --save-dev react-testing-library
108105

109106
This library has a `peerDependencies` listing for `react-dom`.
110107

108+
You may also be interested in installing `dom-testing-library` so you can use
109+
[the custom jest matchers](https://github.com/kentcdodds/dom-testing-library/blob/master/README.md#custom-jest-matchers)
110+
111111
## Usage
112112

113113
```javascript
114114
// __tests__/fetch.js
115115
import React from 'react'
116116
import {render, Simulate, wait} from 'react-testing-library'
117-
import 'react-testing-library/extend-expect' // this adds custom expect matchers
117+
// this add custom expect matchers from dom-testing-library
118+
import 'dom-testing-library/extend-expect'
118119
import axiosMock from 'axios' // the mock lives in a __mocks__ directory
119120
import Fetch from '../fetch' // see the tests for a full implementation
120121

@@ -312,94 +313,6 @@ The default `interval` is `50ms`. However it will run your callback immediately
312313
on the next tick of the event loop (in a `setTimeout`) before starting the
313314
intervals.
314315

315-
## Custom Jest Matchers
316-
317-
There are two simple API which extend the `expect` API of jest for making assertions easier.
318-
319-
### `toBeInTheDOM`
320-
321-
This allows you to assert whether an element present in the DOM or not.
322-
323-
```javascript
324-
// add the custom expect matchers
325-
import 'react-testing-library/extend-expect'
326-
327-
// ...
328-
const {queryByTestId} = render(<span data-testid="count-value">2</span>)
329-
expect(queryByTestId('count-value')).toBeInTheDOM()
330-
expect(queryByTestId('count-value1')).not.toBeInTheDOM()
331-
// ...
332-
```
333-
334-
> Note: when using `toBeInTheDOM`, make sure you use a query function
335-
> (like `queryByTestId`) rather than a get function (like `getByTestId`).
336-
> Otherwise the `get*` function could throw an error before your assertion.
337-
338-
### `toHaveTextContent`
339-
340-
This API allows you to check whether the given element has a text content or not.
341-
342-
```javascript
343-
// add the custom expect matchers
344-
import 'react-testing-library/extend-expect'
345-
346-
// ...
347-
const {getByTestId} = render(<span data-testid="count-value">2</span>)
348-
expect(getByTestId('count-value')).toHaveTextContent('2')
349-
expect(getByTestId('count-value')).not.toHaveTextContent('21')
350-
// ...
351-
```
352-
353-
### `toHaveAttribute`
354-
355-
This allows you to check wether the given element has an attribute or not. You
356-
can also optionally check that the attribute has a specific expected value.
357-
358-
```javascript
359-
// add the custom expect matchers
360-
import 'react-testing-library/extend-expect'
361-
362-
// ...
363-
const {getByTestId} = render(
364-
<button data-testid="ok-button" type="submit" disabled>
365-
OK
366-
</button>,
367-
)
368-
expect(getByTestId('ok-button')).toHaveAttribute('disabled')
369-
expect(getByTestId('ok-button')).toHaveAttribute('type', 'submit')
370-
expect(getByTestId('ok-button')).not.toHaveAttribute('type', 'button')
371-
// ...
372-
```
373-
374-
### Custom Jest Matchers - Typescript
375-
376-
When you use custom Jest Matchers with Typescript, you will need to extend the type signature of `jest.Matchers<void>`, then cast the result of `expect` accordingly. Here's a handy usage example:
377-
378-
```typescript
379-
// this adds custom expect matchers
380-
import 'react-testing-library/extend-expect'
381-
interface ExtendedMatchers extends jest.Matchers<void> {
382-
toHaveTextContent: (htmlElement: string) => object
383-
toBeInTheDOM: () => void
384-
}
385-
test('renders the tooltip as expected', async () => {
386-
const {
387-
// getByLabelText,
388-
getByText,
389-
// getByTestId,
390-
container,
391-
} = render(<Tooltip label="hello world">Child</Tooltip>)
392-
// tests rendering of the child
393-
getByText('Child')
394-
// tests rendering of tooltip label
395-
;(expect(getByText('hello world')) as ExtendedMatchers).toHaveTextContent(
396-
'hello world',
397-
)
398-
// snapshots work great with regular DOM nodes!
399-
expect(container.firstChild).toMatchSnapshot()
400-
})
401-
```
402-
403316
## `TextMatch`
404317

405318
Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
@@ -681,27 +594,6 @@ react components.
681594

682595
</details>
683596

684-
## Deprecated APIs
685-
686-
### `flushPromises`
687-
688-
> This API was deprecated in favor of [`wait`](#wait). We try to avoid having
689-
> two ways to do the same thing and you can accomplish everything with `wait`
690-
> that you could with `flushPromises`. A big advantage of `wait`, is that
691-
> `flushPromises` will not flush promises that have not been queued up already,
692-
> for example, if they will queue up as a result of the initial promises. In
693-
> consequence of that, you might have to call `flushPromises` multiple times to
694-
> get your components to your desired state. You can accomplish the exact same
695-
> behavior with `wait` as you had with `flushPromises` by calling `wait` with
696-
> no arguments: `await wait()`
697-
698-
This is a simple utility that's useful for when your component is doing some
699-
async work that you've mocked out, but you still need to wait until the next
700-
tick of the event loop before you can continue your assertions. It simply
701-
returns a promise that resolves in a `setImmediate`. Especially useful when
702-
you make your test function an `async` function and use
703-
`await flushPromises()`.
704-
705597
## Other Solutions
706598

707599
In preparing this project,

extend-expect.js

-1
This file was deleted.

package.json

+14-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,24 @@
1919
},
2020
"files": [
2121
"dist",
22-
"typings",
23-
"extend-expect.js"
22+
"typings"
23+
],
24+
"keywords": [
25+
"testing",
26+
"react",
27+
"ui",
28+
"dom",
29+
"jsdom",
30+
"unit",
31+
"integration",
32+
"functional",
33+
"end-to-end",
34+
"e2e"
2435
],
25-
"keywords": [],
2636
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)",
2737
"license": "MIT",
2838
"dependencies": {
39+
"dom-testing-library": "^1.0.0",
2940
"wait-for-expect": "0.4.0"
3041
},
3142
"devDependencies": {

src/__tests__/__snapshots__/element-queries.js.snap

-15
This file was deleted.

src/__tests__/deprecated.js

-14
This file was deleted.

src/__tests__/element-queries.js

-150
This file was deleted.

0 commit comments

Comments
 (0)