Skip to content

Restore prettyDOM logging for Cypress #1018

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

Merged
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
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}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the handling of prettifiedDOM.length === 0 from the default getElementError function. From what I could tell, the only case where that was true was when the function was called from within Cypress. Since we've removed the Cypress special-casing in this PR, this branch no longer seemed useful.

]
.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