File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
9
9
10
10
For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
11
11
12
- _ 948 TILs and counting..._
12
+ _ 949 TILs and counting..._
13
13
14
14
---
15
15
@@ -692,6 +692,7 @@ _948 TILs and counting..._
692
692
### React Testing Library
693
693
694
694
- [ 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 )
695
696
- [ Test A Component That Uses React Portals] ( react-testing-library/test-a-component-that-uses-react-portals.md )
696
697
697
698
### ReasonML
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments