Skip to content

Commit 613fc5d

Browse files
committed
Add Pretty Print Some Dom To Debug A Test as a RTL til
1 parent 1c71bc9 commit 613fc5d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_948 TILs and counting..._
12+
_949 TILs and counting..._
1313

1414
---
1515

@@ -692,6 +692,7 @@ _948 TILs and counting..._
692692
### React Testing Library
693693

694694
- [findBy\* Queries Have Async Built In](react-testing-library/find-by-queries-have-async-built-in.md)
695+
- [Pretty Print Some DOM To Debug A Test](react-testing-library/pretty-print-some-dom-to-debug-a-test.md)
695696
- [Test A Component That Uses React Portals](react-testing-library/test-a-component-that-uses-react-portals.md)
696697

697698
### ReasonML
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Pretty Print Some DOM To Debug A Test
2+
3+
Our test's assertions can help guide what needs to change in the code.
4+
Sometimes those test failures can be too opaque to be helpful.
5+
6+
It'd be easier if we could just take a peek at how the component is rendering.
7+
8+
```javascript
9+
import { render, screen } from "@testing-library/react";
10+
import { prettyDOM } from "@testing-library/dom";
11+
import "@testing-library/jest-dom/extend-expect";
12+
13+
import MyComponent from "../MyComponent";
14+
15+
test("renders MyComponent", () => {
16+
const { container } = render(<MyComponent />);
17+
18+
console.log(prettyDOM(container));
19+
20+
const nameInput = screen.getByLabelText("Name");
21+
22+
console.log(prettyDOM(nameInput));
23+
24+
// some expectations
25+
});
26+
```
27+
28+
Passing the rendered container or elements that we've queried for to the
29+
[`prettyDOM`](https://testing-library.com/docs/dom-testing-library/api-helpers#prettydom)
30+
utility creates a formatted, syntax-highlighted version of that part of the
31+
virtual DOM (without all the React Internal noise). This can then be logged out
32+
for debugging purposes.

0 commit comments

Comments
 (0)