Skip to content

Commit 76cb73d

Browse files
authored
feat: Reduce caught exceptions in prettyDom (#1321)
Co-authored-by: David Rieman <[email protected]>
1 parent 0a8ad65 commit 76cb73d

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/__tests__/pretty-dom.js

+42-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ test('prettyDOM can include all elements with a custom filter', () => {
155155

156156
test('prettyDOM supports a COLORS environment variable', () => {
157157
const {container} = render('<div>Hello World!</div>')
158-
159158
const noColors = prettyDOM(container, undefined, {highlight: false})
160159
const withColors = prettyDOM(container, undefined, {highlight: true})
161160

@@ -167,6 +166,48 @@ test('prettyDOM supports a COLORS environment variable', () => {
167166
expect(prettyDOM(container)).toEqual(withColors)
168167
})
169168

169+
test('prettyDOM handles a COLORS env variable of unexpected object type by colorizing for node', () => {
170+
const {container} = render('<div>Hello World!</div>')
171+
const noColors = prettyDOM(container, undefined, {highlight: false})
172+
const withColors = prettyDOM(container, undefined, {highlight: true})
173+
174+
const originalNodeVersion = process.versions.node
175+
process.env.COLORS = '{}'
176+
delete process.versions.node
177+
expect(prettyDOM(container)).toEqual(noColors)
178+
process.versions.node = '1.2.3'
179+
expect(prettyDOM(container)).toEqual(withColors)
180+
process.versions.node = originalNodeVersion
181+
})
182+
183+
test('prettyDOM handles a COLORS env variable of undefined by colorizing for node', () => {
184+
const {container} = render('<div>Hello World!</div>')
185+
const noColors = prettyDOM(container, undefined, {highlight: false})
186+
const withColors = prettyDOM(container, undefined, {highlight: true})
187+
188+
const originalNodeVersion = process.versions.node
189+
process.env.COLORS = undefined
190+
delete process.versions.node
191+
expect(prettyDOM(container)).toEqual(noColors)
192+
process.versions.node = '1.2.3'
193+
expect(prettyDOM(container)).toEqual(withColors)
194+
process.versions.node = originalNodeVersion
195+
})
196+
197+
test('prettyDOM handles a COLORS env variable of empty string by colorizing for node', () => {
198+
const {container} = render('<div>Hello World!</div>')
199+
const noColors = prettyDOM(container, undefined, {highlight: false})
200+
const withColors = prettyDOM(container, undefined, {highlight: true})
201+
202+
const originalNodeVersion = process.versions.node
203+
process.env.COLORS = ''
204+
delete process.versions.node
205+
expect(prettyDOM(container)).toEqual(noColors)
206+
process.versions.node = '1.2.3'
207+
expect(prettyDOM(container)).toEqual(withColors)
208+
process.versions.node = originalNodeVersion
209+
})
210+
170211
test('prettyDOM supports named custom elements', () => {
171212
window.customElements.define(
172213
'my-element-1',

src/pretty-dom.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ import {getDocument} from './helpers'
55
import {getConfig} from './config'
66

77
const shouldHighlight = () => {
8-
let colors
8+
// Try to safely parse env COLORS: We will default behavior if any step fails.
99
try {
10-
colors = JSON.parse(process?.env?.COLORS)
11-
} catch (e) {
12-
// If this throws, process?.env?.COLORS wasn't parsable. Since we only
13-
// care about `true` or `false`, we can safely ignore the error.
10+
const colors = process?.env?.COLORS
11+
if (colors) {
12+
const b = JSON.parse(colors)
13+
if (typeof b === 'boolean') return b
14+
}
15+
} catch {
16+
// Ignore (non-critical) - Make a defaulting choice below.
1417
}
1518

16-
if (typeof colors === 'boolean') {
17-
// If `colors` is set explicitly (both `true` and `false`), use that value.
18-
return colors
19-
} else {
20-
// If `colors` is not set, colorize if we're in node.
21-
return (
22-
typeof process !== 'undefined' &&
23-
process.versions !== undefined &&
24-
process.versions.node !== undefined
25-
)
26-
}
19+
// In all other cases, whether COLORS was a weird type, or the attempt threw:
20+
// Fall back to colorizing if we are running in node.
21+
return !!process?.versions?.node
2722
}
2823

2924
const {DOMCollection} = prettyFormat.plugins

0 commit comments

Comments
 (0)