Skip to content

Commit f52e794

Browse files
committed
fix: Access process only if defined
1 parent be5d9c1 commit f52e794

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

src/__tests__/pretty-dom.js

+44-45
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ function prettyDOM(...args) {
99
throw new Error('process is no longer defined. Remove this setup code.')
1010
} else {
1111
originalProcess = process
12-
// TODO: Delete to test browser environments
13-
// delete globalThis.process
12+
delete globalThis.process
1413
}
1514

1615
try {
@@ -23,11 +22,11 @@ function prettyDOM(...args) {
2322
test('prettyDOM prints out the given DOM element tree highlighted', () => {
2423
const {container} = render('<div>Hello World!</div>')
2524
expect(prettyDOM(container)).toMatchInlineSnapshot(`
26-
[36m<div>[39m
27-
[36m<div>[39m
28-
[0mHello World![0m
29-
[36m</div>[39m
30-
[36m</div>[39m
25+
<div>
26+
<div>
27+
Hello World!
28+
</div>
29+
</div>
3130
`)
3231
})
3332

@@ -36,21 +35,21 @@ test('prettyDOM supports truncating the output length', () => {
3635
expect(prettyDOM(container, 5)).toMatch(/\.\.\./)
3736
expect(prettyDOM(container, 0)).toMatch('')
3837
expect(prettyDOM(container, Number.POSITIVE_INFINITY)).toMatchInlineSnapshot(`
39-
[36m<div>[39m
40-
[36m<div>[39m
41-
[0mHello World![0m
42-
[36m</div>[39m
43-
[36m</div>[39m
38+
<div>
39+
<div>
40+
Hello World!
41+
</div>
42+
</div>
4443
`)
4544
})
4645

4746
test('prettyDOM defaults to document.body', () => {
4847
const defaultInlineSnapshot = `
49-
[36m<body>[39m
50-
[36m<div>[39m
51-
[0mHello World![0m
52-
[36m</div>[39m
53-
[36m</body>[39m
48+
<body>
49+
<div>
50+
Hello World!
51+
</div>
52+
</body>
5453
`
5554
renderIntoDocument('<div>Hello World!</div>')
5655
expect(prettyDOM()).toMatchInlineSnapshot(defaultInlineSnapshot)
@@ -59,10 +58,10 @@ test('prettyDOM defaults to document.body', () => {
5958

6059
test('prettyDOM supports receiving the document element', () => {
6160
expect(prettyDOM(document)).toMatchInlineSnapshot(`
62-
[36m<html>[39m
63-
[36m<head />[39m
64-
[36m<body />[39m
65-
[36m</html>[39m
61+
<html>
62+
<head />
63+
<body />
64+
</html>
6665
`)
6766
})
6867

@@ -90,11 +89,11 @@ test('prettyDOM ignores script elements and comments nodes by default', () => {
9089
)
9190

9291
expect(prettyDOM(container)).toMatchInlineSnapshot(`
93-
[36m<body>[39m
94-
[36m<p>[39m
95-
[0mHello, Dave[0m
96-
[36m</p>[39m
97-
[36m</body>[39m
92+
<body>
93+
<p>
94+
Hello, Dave
95+
</p>
96+
</body>
9897
`)
9998
})
10099

@@ -106,15 +105,15 @@ test('prettyDOM can include all elements with a custom filter', () => {
106105
expect(
107106
prettyDOM(container, Number.POSITIVE_INFINITY, {filterNode: () => true}),
108107
).toMatchInlineSnapshot(`
109-
[36m<body>[39m
110-
[36m<script[39m
111-
[33msrc[39m=[32m"context.js"[39m
112-
[36m/>[39m
113-
[90m<!-- Some comment -->[39m
114-
[36m<p>[39m
115-
[0mHello, Dave[0m
116-
[36m</p>[39m
117-
[36m</body>[39m
108+
<body>
109+
<script
110+
src="context.js"
111+
/>
112+
<!-- Some comment -->
113+
<p>
114+
Hello, Dave
115+
</p>
116+
</body>
118117
`)
119118
})
120119

@@ -127,11 +126,11 @@ test('prettyDOM supports named custom elements', () => {
127126
const {container} = render('<my-element-1>Hello World!</my-element-1>')
128127

129128
expect(prettyDOM(container)).toMatchInlineSnapshot(`
130-
[36m<div>[39m
131-
[36m<my-element-1>[39m
132-
[0mHello World![0m
133-
[36m</my-element-1>[39m
134-
[36m</div>[39m
129+
<div>
130+
<my-element-1>
131+
Hello World!
132+
</my-element-1>
133+
</div>
135134
`)
136135
})
137136

@@ -141,10 +140,10 @@ test('prettyDOM supports anonymous custom elements', () => {
141140
const {container} = render('<my-element-2>Hello World!</my-element-2>')
142141

143142
expect(prettyDOM(container)).toMatchInlineSnapshot(`
144-
[36m<div>[39m
145-
[36m<my-element-2>[39m
146-
[0mHello World![0m
147-
[36m</my-element-2>[39m
148-
[36m</div>[39m
143+
<div>
144+
<my-element-2>
145+
Hello World!
146+
</my-element-2>
147+
</div>
149148
`)
150149
})

src/pretty-dom.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import {getDocument} from './helpers'
55
import {getConfig} from './config'
66

77
const shouldHighlight = () => {
8+
if (typeof process === 'undefined') {
9+
// Don't add ANSI colors outside of Node.js (e.g. in browsers).
10+
return false
11+
}
812
// Try to safely parse env COLORS: We will default behavior if any step fails.
913
try {
10-
const colors = process?.env?.COLORS
14+
const colors = process.env?.COLORS
1115
if (colors) {
1216
const b = JSON.parse(colors)
1317
if (typeof b === 'boolean') return b
@@ -18,7 +22,7 @@ const shouldHighlight = () => {
1822

1923
// In all other cases, whether COLORS was a weird type, or the attempt threw:
2024
// Fall back to colorizing if we are running in node.
21-
return !!process?.versions?.node
25+
return Boolean(process.versions?.node)
2226
}
2327

2428
const {DOMCollection} = prettyFormat.plugins

0 commit comments

Comments
 (0)