Skip to content

Commit de769ec

Browse files
author
Kent C. Dodds
authored
feat(debug): add a debug convenience method (#87)
Also adds @Gpx to contributors! Closes #86
1 parent ad4627a commit de769ec

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

.all-contributorsrc

+10
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@
256256
"bug",
257257
"code"
258258
]
259+
},
260+
{
261+
"login": "Gpx",
262+
"name": "Giorgio Polvara",
263+
"avatar_url": "https://avatars0.githubusercontent.com/u/767959?v=4",
264+
"profile": "https://twitter.com/Gpx",
265+
"contributions": [
266+
"bug",
267+
"ideas"
268+
]
259269
}
260270
]
261271
}

README.md

+45-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-25-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-26-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]
@@ -88,6 +88,7 @@ facilitate testing implementation details). Read more about this in
8888
* [`wait`](#wait)
8989
* [`waitForElement`](#waitforelement)
9090
* [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
91+
* [`prettyDOM`](#prettydom)
9192
* [`TextMatch`](#textmatch)
9293
* [`query` APIs](#query-apis)
9394
* [Examples](#examples)
@@ -130,7 +131,7 @@ import Fetch from '../fetch' // see the tests for a full implementation
130131

131132
test('Fetch makes an API call and displays the greeting when load-greeting is clicked', async () => {
132133
// Arrange
133-
axiosMock.get.mockResolvedValueOnce({ data: { greeting: 'hello there' } })
134+
axiosMock.get.mockResolvedValueOnce({data: {greeting: 'hello there'}})
134135
const url = '/greeting'
135136
const {getByText, getByTestId, container} = render(<Fetch url={url} />)
136137

@@ -164,6 +165,21 @@ The containing DOM node of your rendered React Element (rendered using
164165

165166
> Tip: To get the root element of your rendered element, use `container.firstChild`.
166167
168+
#### `debug`
169+
170+
This method is a shortcut for `console.log(prettyDOM(container))`.
171+
172+
```javascript
173+
const HelloWorld = () => <h1>Hello World</h1>
174+
const {debug} = render(<HelloWorld />)
175+
debug()
176+
// <div>
177+
// <h1>Hello World</h1>
178+
// </div>
179+
```
180+
181+
Learn more about [`prettyDOM`](#prettydom) below.
182+
167183
#### `rerender`
168184

169185
It'd probably be better if you test the component that's doing the prop updating
@@ -468,6 +484,32 @@ fireEvent.click(getElementByText('Submit'), rightClick)
468484
// default `button` property for click events is set to `0` which is a left click.
469485
```
470486

487+
### `prettyDOM`
488+
489+
This helper function can be used to print out readable representation of the DOM
490+
tree of a node. This can be helpful for instance when debugging tests.
491+
492+
It is defined as:
493+
494+
```typescript
495+
function prettyDOM(node: HTMLElement, maxLength?: number): string
496+
```
497+
498+
It receives the root node to print out, and an optional extra argument to limit
499+
the size of the resulting string, for cases when it becomes too large.
500+
501+
This function is usually used alongside `console.log` to temporarily print out
502+
DOM trees during tests for debugging purposes:
503+
504+
```javascript
505+
const HelloWorld = () => <h1>Hello World</h1>
506+
const {container} = render(<HelloWorld />)
507+
console.log(prettyDOM(container))
508+
// <div>
509+
// <h1>Hello World</h1>
510+
// </div>
511+
```
512+
471513
## `TextMatch`
472514
473515
Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
@@ -808,7 +850,7 @@ Thanks goes to these people ([emoji key][emojis]):
808850
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
809851
| [<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") |
810852
| [<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") |
811-
| [<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") |
853+
| [<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") |
812854
813855
<!-- ALL-CONTRIBUTORS-LIST:END -->
814856

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"extends": "./node_modules/kcd-scripts/eslint.js",
6363
"rules": {
6464
"react/prop-types": "off",
65-
"import/no-unassigned-import": "off"
65+
"import/no-unassigned-import": "off",
66+
"import/named": "off"
6667
}
6768
},
6869
"eslintIgnore": [

src/__tests__/debug.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react'
2+
import {render} from '../'
3+
4+
beforeEach(() => {
5+
jest.spyOn(console, 'log').mockImplementation(() => {})
6+
})
7+
8+
afterEach(() => {
9+
console.log.mockRestore()
10+
})
11+
12+
test('debug pretty prints the container', () => {
13+
const HelloWorld = () => <h1>Hello World</h1>
14+
const {debug} = render(<HelloWorld />)
15+
debug()
16+
expect(console.log).toHaveBeenCalledTimes(1)
17+
expect(console.log).toHaveBeenCalledWith(
18+
expect.stringContaining('Hello World'),
19+
)
20+
})
21+
22+
/* eslint no-console:0 */

src/index.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import ReactDOM from 'react-dom'
22
import {Simulate} from 'react-dom/test-utils'
3-
import {bindElementToQueries, wait, fireEvent, waitForElement} from 'dom-testing-library'
3+
import {bindElementToQueries, prettyDOM} from 'dom-testing-library'
44

55
function render(ui, {container = document.createElement('div')} = {}) {
66
ReactDOM.render(ui, container)
77
return {
88
container,
9+
// eslint-disable-next-line no-console
10+
debug: () => console.log(prettyDOM(container)),
911
unmount: () => ReactDOM.unmountComponentAtNode(container),
1012
rerender: rerenderUi => {
1113
render(rerenderUi, {container})
@@ -40,12 +42,6 @@ syntheticEvents.forEach(eventName => {
4042
})
4143
})
4244

43-
export {
44-
render,
45-
Simulate,
46-
wait,
47-
waitForElement,
48-
fireEvent,
49-
renderIntoDocument,
50-
cleanup,
51-
}
45+
// just re-export everything from dom-testing-library
46+
export * from 'dom-testing-library'
47+
export {render, Simulate, renderIntoDocument, cleanup}

0 commit comments

Comments
 (0)