Skip to content

Commit fdc12ec

Browse files
authored
fix: Revert "feat: Reduce caught exceptions in prettyDom (#1321)" (#1325)
This reverts commit 76cb73d.
1 parent 76cb73d commit fdc12ec

File tree

2 files changed

+17
-53
lines changed

2 files changed

+17
-53
lines changed

src/__tests__/pretty-dom.js

+1-42
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ 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+
158159
const noColors = prettyDOM(container, undefined, {highlight: false})
159160
const withColors = prettyDOM(container, undefined, {highlight: true})
160161

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

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-
211170
test('prettyDOM supports named custom elements', () => {
212171
window.customElements.define(
213172
'my-element-1',

src/pretty-dom.js

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

77
const shouldHighlight = () => {
8-
// Try to safely parse env COLORS: We will default behavior if any step fails.
8+
let colors
99
try {
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.
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.
1714
}
1815

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
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+
}
2227
}
2328

2429
const {DOMCollection} = prettyFormat.plugins

0 commit comments

Comments
 (0)