1
- import { prettyDOM } from '../pretty-dom'
1
+ import { prettyDOM as prettyDOMImpl } from '../pretty-dom'
2
2
import { render , renderIntoDocument } from './helpers/test-utils'
3
3
4
- test ( 'prettyDOM prints out the given DOM element tree' , ( ) => {
4
+ function prettyDOM ( ...args ) {
5
+ let originalProcess
6
+ // this shouldn't be defined in this environment in the first place
7
+ if ( typeof process !== 'undefined' ) {
8
+ originalProcess = process
9
+ // TODO: Delete to test browser environments
10
+ // delete globalThis.process
11
+ } else {
12
+ throw new Error ( 'process is no longer defined. Remove this setup code.' )
13
+ }
14
+
15
+ try {
16
+ return prettyDOMImpl ( ...args )
17
+ } finally {
18
+ globalThis . process = originalProcess
19
+ }
20
+ }
21
+
22
+ test ( 'prettyDOM prints out the given DOM element tree highlighted' , ( ) => {
5
23
const { container} = render ( '<div>Hello World!</div>' )
6
24
expect ( prettyDOM ( container ) ) . toMatchInlineSnapshot ( `
7
- <div>
8
- <div>
9
- Hello World!
10
- </div>
11
- </div>
25
+ [36m <div>[39m
26
+ [36m <div>[39m
27
+ [0mHello World![0m
28
+ [36m </div>[39m
29
+ [36m </div>[39m
12
30
` )
13
31
} )
14
32
@@ -17,21 +35,21 @@ test('prettyDOM supports truncating the output length', () => {
17
35
expect ( prettyDOM ( container , 5 ) ) . toMatch ( / \. \. \. / )
18
36
expect ( prettyDOM ( container , 0 ) ) . toMatch ( '' )
19
37
expect ( prettyDOM ( container , Number . POSITIVE_INFINITY ) ) . toMatchInlineSnapshot ( `
20
- <div>
21
- <div>
22
- Hello World!
23
- </div>
24
- </div>
38
+ [36m <div>[39m
39
+ [36m <div>[39m
40
+ [0mHello World![0m
41
+ [36m </div>[39m
42
+ [36m </div>[39m
25
43
` )
26
44
} )
27
45
28
46
test ( 'prettyDOM defaults to document.body' , ( ) => {
29
47
const defaultInlineSnapshot = `
30
- <body>
31
- <div>
32
- Hello World!
33
- </div>
34
- </body>
48
+ [36m <body>[39m
49
+ [36m <div>[39m
50
+ [0mHello World![0m
51
+ [36m </div>[39m
52
+ [36m </body>[39m
35
53
`
36
54
renderIntoDocument ( '<div>Hello World!</div>' )
37
55
expect ( prettyDOM ( ) ) . toMatchInlineSnapshot ( defaultInlineSnapshot )
@@ -40,10 +58,10 @@ test('prettyDOM defaults to document.body', () => {
40
58
41
59
test ( 'prettyDOM supports receiving the document element' , ( ) => {
42
60
expect ( prettyDOM ( document ) ) . toMatchInlineSnapshot ( `
43
- <html>
44
- <head />
45
- <body />
46
- </html>
61
+ [36m <html>[39m
62
+ [36m <head />[39m
63
+ [36m <body />[39m
64
+ [36m </html>[39m
47
65
` )
48
66
} )
49
67
@@ -71,11 +89,11 @@ test('prettyDOM ignores script elements and comments nodes by default', () => {
71
89
)
72
90
73
91
expect ( prettyDOM ( container ) ) . toMatchInlineSnapshot ( `
74
- <body>
75
- <p>
76
- Hello , Dave
77
- </p>
78
- </body>
92
+ [36m <body>[39m
93
+ [36m <p>[39m
94
+ [0mHello , Dave[0m
95
+ [36m </p>[39m
96
+ [36m </body>[39m
79
97
` )
80
98
} )
81
99
@@ -87,73 +105,18 @@ test('prettyDOM can include all elements with a custom filter', () => {
87
105
expect (
88
106
prettyDOM ( container , Number . POSITIVE_INFINITY , { filterNode : ( ) => true } ) ,
89
107
) . toMatchInlineSnapshot ( `
90
- <body>
91
- <script
92
- src= "context.js"
93
- />
94
- <!-- Some comment -->
95
- <p>
96
- Hello , Dave
97
- </p>
98
- </body>
108
+ [36m <body>[39m
109
+ [36m <script[39m
110
+ [33msrc[39m=[32m "context.js"[39m
111
+ [36m/>[39m
112
+ [90m <!-- Some comment -->[39m
113
+ [36m <p>[39m
114
+ [0mHello , Dave[0m
115
+ [36m </p>[39m
116
+ [36m </body>[39m
99
117
` )
100
118
} )
101
119
102
- test ( 'prettyDOM supports a COLORS environment variable' , ( ) => {
103
- const { container} = render ( '<div>Hello World!</div>' )
104
- const noColors = prettyDOM ( container , undefined , { highlight : false } )
105
- const withColors = prettyDOM ( container , undefined , { highlight : true } )
106
-
107
- // process.env.COLORS is a string, so make sure we test it as such
108
- process . env . COLORS = 'false'
109
- expect ( prettyDOM ( container ) ) . toEqual ( noColors )
110
-
111
- process . env . COLORS = 'true'
112
- expect ( prettyDOM ( container ) ) . toEqual ( withColors )
113
- } )
114
-
115
- test ( 'prettyDOM handles a COLORS env variable of unexpected object type by colorizing for node' , ( ) => {
116
- const { container} = render ( '<div>Hello World!</div>' )
117
- const noColors = prettyDOM ( container , undefined , { highlight : false } )
118
- const withColors = prettyDOM ( container , undefined , { highlight : true } )
119
-
120
- const originalNodeVersion = process . versions . node
121
- process . env . COLORS = '{}'
122
- delete process . versions . node
123
- expect ( prettyDOM ( container ) ) . toEqual ( noColors )
124
- process . versions . node = '1.2.3'
125
- expect ( prettyDOM ( container ) ) . toEqual ( withColors )
126
- process . versions . node = originalNodeVersion
127
- } )
128
-
129
- test ( 'prettyDOM handles a COLORS env variable of undefined by colorizing for node' , ( ) => {
130
- const { container} = render ( '<div>Hello World!</div>' )
131
- const noColors = prettyDOM ( container , undefined , { highlight : false } )
132
- const withColors = prettyDOM ( container , undefined , { highlight : true } )
133
-
134
- const originalNodeVersion = process . versions . node
135
- process . env . COLORS = undefined
136
- delete process . versions . node
137
- expect ( prettyDOM ( container ) ) . toEqual ( noColors )
138
- process . versions . node = '1.2.3'
139
- expect ( prettyDOM ( container ) ) . toEqual ( withColors )
140
- process . versions . node = originalNodeVersion
141
- } )
142
-
143
- test ( 'prettyDOM handles a COLORS env variable of empty string by colorizing for node' , ( ) => {
144
- const { container} = render ( '<div>Hello World!</div>' )
145
- const noColors = prettyDOM ( container , undefined , { highlight : false } )
146
- const withColors = prettyDOM ( container , undefined , { highlight : true } )
147
-
148
- const originalNodeVersion = process . versions . node
149
- process . env . COLORS = ''
150
- delete process . versions . node
151
- expect ( prettyDOM ( container ) ) . toEqual ( noColors )
152
- process . versions . node = '1.2.3'
153
- expect ( prettyDOM ( container ) ) . toEqual ( withColors )
154
- process . versions . node = originalNodeVersion
155
- } )
156
-
157
120
test ( 'prettyDOM supports named custom elements' , ( ) => {
158
121
window . customElements . define (
159
122
'my-element-1' ,
@@ -163,11 +126,11 @@ test('prettyDOM supports named custom elements', () => {
163
126
const { container} = render ( '<my-element-1>Hello World!</my-element-1>' )
164
127
165
128
expect ( prettyDOM ( container ) ) . toMatchInlineSnapshot ( `
166
- <div>
167
- <my-element-1>
168
- Hello World!
169
- </my-element-1>
170
- </div>
129
+ [36m <div>[39m
130
+ [36m <my-element-1>[39m
131
+ [0mHello World![0m
132
+ [36m </my-element-1>[39m
133
+ [36m </div>[39m
171
134
` )
172
135
} )
173
136
@@ -177,10 +140,10 @@ test('prettyDOM supports anonymous custom elements', () => {
177
140
const { container} = render ( '<my-element-2>Hello World!</my-element-2>' )
178
141
179
142
expect ( prettyDOM ( container ) ) . toMatchInlineSnapshot ( `
180
- <div>
181
- <my-element-2>
182
- Hello World!
183
- </my-element-2>
184
- </div>
143
+ [36m <div>[39m
144
+ [36m <my-element-2>[39m
145
+ [0mHello World![0m
146
+ [36m </my-element-2>[39m
147
+ [36m </div>[39m
185
148
` )
186
149
} )
0 commit comments