Skip to content

Commit

Permalink
feat: restore prettyDOM logging for Cypress (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
istateside committed Sep 7, 2021
1 parent bb79e30 commit 460115a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 64 deletions.
44 changes: 0 additions & 44 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {configure} from '../config'
import {render, renderIntoDocument} from './helpers/test-utils'

beforeEach(() => {
document.defaultView.Cypress = null
})

test('query can return null', () => {
const {
queryByLabelText,
Expand Down Expand Up @@ -1061,46 +1057,6 @@ test('the debug helper prints the dom state here', () => {
process.env.DEBUG_PRINT_LIMIT = originalDebugPrintLimit
})

test('get throws a useful error message without DOM in Cypress', () => {
document.defaultView.Cypress = {}
const {
getByLabelText,
getByPlaceholderText,
getByText,
getByTestId,
getByAltText,
getByTitle,
getByDisplayValue,
} = render('<div />')
expect(() =>
getByLabelText('LucyRicardo'),
).toThrowErrorMatchingInlineSnapshot(
`Unable to find a label with the text of: LucyRicardo`,
)
expect(() =>
getByPlaceholderText('LucyRicardo'),
).toThrowErrorMatchingInlineSnapshot(
`Unable to find an element with the placeholder text of: LucyRicardo`,
)
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
`Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.`,
)
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
`Unable to find an element by: [data-testid="LucyRicardo"]`,
)
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
`Unable to find an element with the alt text: LucyRicardo`,
)
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
`Unable to find an element with the title: LucyRicardo.`,
)
expect(() =>
getByDisplayValue('LucyRicardo'),
).toThrowErrorMatchingInlineSnapshot(
`Unable to find an element with the display value: LucyRicardo.`,
)
})

test('getByText ignores script tags by default', () => {
const {getAllByText} = render(
'<script>Hello</script><div>Hello</div><style>.Hello{}</style>',
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ test('prettyDOM prints out the given DOM element tree', () => {
test('prettyDOM supports truncating the output length', () => {
const {container} = render('<div>Hello World!</div>')
expect(prettyDOM(container, 5)).toMatch(/\.\.\./)
expect(prettyDOM(container, 0)).toMatch('')
expect(prettyDOM(container, Number.POSITIVE_INFINITY)).toMatchInlineSnapshot(`
"<div>
<div>
Hello World!
</div>
</div>"
`)
})

test('prettyDOM defaults to document.body', () => {
Expand Down
6 changes: 1 addition & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ let config: InternalConfig = {
const error = new Error(
[
message,
prettifiedDOM.length > 0
? `Ignored nodes: comments, <script />, <style />\n${prettyDOM(
container,
)}`
: null,
`Ignored nodes: comments, <script />, <style />\n${prettifiedDOM}`,
]
.filter(Boolean)
.join('\n\n'),
Expand Down
17 changes: 2 additions & 15 deletions src/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,11 @@ import {getUserCodeFrame} from './get-user-code-frame'
import {getDocument} from './helpers'
import {DEFAULT_IGNORE_TAGS} from './shared'

function inCypress(dom) {
const window =
(dom.ownerDocument && dom.ownerDocument.defaultView) || undefined
return (
(typeof global !== 'undefined' && global.Cypress) ||
(typeof window !== 'undefined' && window.Cypress)
)
}

const inNode = () =>
typeof process !== 'undefined' &&
process.versions !== undefined &&
process.versions.node !== undefined

const getMaxLength = dom =>
inCypress(dom)
? 0
: (typeof process !== 'undefined' && process.env.DEBUG_PRINT_LIMIT) || 7000

const {DOMCollection} = prettyFormat.plugins

// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node_type_constants
Expand All @@ -43,7 +29,8 @@ function prettyDOM(dom, maxLength, options = {}) {
dom = getDocument().body
}
if (typeof maxLength !== 'number') {
maxLength = getMaxLength(dom)
maxLength =
(typeof process !== 'undefined' && process.env.DEBUG_PRINT_LIMIT) || 7000
}

if (maxLength === 0) {
Expand Down

0 comments on commit 460115a

Please sign in to comment.