Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Reduce caught exceptions in prettyDom #1321

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/__tests__/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ test('prettyDOM can include all elements with a custom filter', () => {

test('prettyDOM supports a COLORS environment variable', () => {
const {container} = render('<div>Hello World!</div>')

const noColors = prettyDOM(container, undefined, {highlight: false})
const withColors = prettyDOM(container, undefined, {highlight: true})

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

test('prettyDOM handles a COLORS env variable of unexpected object type by colorizing for node', () => {
const {container} = render('<div>Hello World!</div>')
const noColors = prettyDOM(container, undefined, {highlight: false})
const withColors = prettyDOM(container, undefined, {highlight: true})

const originalNodeVersion = process.versions.node
process.env.COLORS = '{}'
delete process.versions.node
expect(prettyDOM(container)).toEqual(noColors)
process.versions.node = '1.2.3'
expect(prettyDOM(container)).toEqual(withColors)
process.versions.node = originalNodeVersion
})

test('prettyDOM handles a COLORS env variable of undefined by colorizing for node', () => {
const {container} = render('<div>Hello World!</div>')
const noColors = prettyDOM(container, undefined, {highlight: false})
const withColors = prettyDOM(container, undefined, {highlight: true})

const originalNodeVersion = process.versions.node
process.env.COLORS = undefined
delete process.versions.node
expect(prettyDOM(container)).toEqual(noColors)
process.versions.node = '1.2.3'
expect(prettyDOM(container)).toEqual(withColors)
process.versions.node = originalNodeVersion
})

test('prettyDOM handles a COLORS env variable of empty string by colorizing for node', () => {
const {container} = render('<div>Hello World!</div>')
const noColors = prettyDOM(container, undefined, {highlight: false})
const withColors = prettyDOM(container, undefined, {highlight: true})

const originalNodeVersion = process.versions.node
process.env.COLORS = ''
delete process.versions.node
expect(prettyDOM(container)).toEqual(noColors)
process.versions.node = '1.2.3'
expect(prettyDOM(container)).toEqual(withColors)
process.versions.node = originalNodeVersion
})

test('prettyDOM supports named custom elements', () => {
window.customElements.define(
'my-element-1',
Expand Down
27 changes: 11 additions & 16 deletions src/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@ import {getDocument} from './helpers'
import {getConfig} from './config'

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

if (typeof colors === 'boolean') {
// If `colors` is set explicitly (both `true` and `false`), use that value.
return colors
} else {
// If `colors` is not set, colorize if we're in node.
return (
typeof process !== 'undefined' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always needs to stay for environments that don't have process e.g. browsers: #1323

process.versions !== undefined &&
process.versions.node !== undefined
)
}
// In all other cases, whether COLORS was a weird type, or the attempt threw:
// Fall back to colorizing if we are running in node.
return !!process?.versions?.node
}

const {DOMCollection} = prettyFormat.plugins
Expand Down
Loading