-
Notifications
You must be signed in to change notification settings - Fork 469
Improved logging #45
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
Improved logging #45
Changes from 2 commits
09971d1
5d61778
3ee7860
0e47a90
0801969
7add25f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,17 @@ import {getNodeText} from './get-node-text' | |
import {prettyDOM} from './pretty-dom' | ||
|
||
function debugDOM(htmlElement) { | ||
return prettyDOM(htmlElement, process.env.DEBUG_PRINT_LIMIT || 7000) | ||
const limit = process.env.DEBUG_PRINT_LIMIT || 7000 | ||
const inNode = (typeof module !== 'undefined' && module.exports) | ||
const inBrowser = (typeof window !== 'undefined' && window.document) | ||
const inCypress = (typeof window !== 'undefined' && window.Cypress) | ||
if (inCypress) { | ||
return '' | ||
} | ||
if (inBrowser && !inNode) { | ||
return `\n\n${prettyDOM(htmlElement, limit, { highlight: false })}` | ||
} | ||
return `\n\n${prettyDOM(htmlElement, limit)}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than adding the throw new Error(['Unable to find... etc...', debugDOM(container)].filter(Boolean).join('\n\n')) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Cypress, that would result in having two empty lines at the end of the error message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated my comment to avoid that issue |
||
} | ||
|
||
// Here are the queries for the library. | ||
|
@@ -140,7 +150,7 @@ function getAllByTestId(container, id, ...rest) { | |
const els = queryAllByTestId(container, id, ...rest) | ||
if (!els.length) { | ||
throw new Error( | ||
`Unable to find an element by: [data-testid="${id}"] \n\n${debugDOM( | ||
`Unable to find an element by: [data-testid="${id}"] ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
|
@@ -156,7 +166,7 @@ function getAllByTitle(container, title, ...rest) { | |
const els = queryAllByTitle(container, title, ...rest) | ||
if (!els.length) { | ||
throw new Error( | ||
`Unable to find an element with the title: ${title}. \n\n${debugDOM( | ||
`Unable to find an element with the title: ${title}. ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
|
@@ -172,7 +182,7 @@ function getAllByValue(container, value, ...rest) { | |
const els = queryAllByValue(container, value, ...rest) | ||
if (!els.length) { | ||
throw new Error( | ||
`Unable to find an element with the value: ${value}. \n\n${debugDOM( | ||
`Unable to find an element with the value: ${value}. ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
|
@@ -188,7 +198,7 @@ function getAllByPlaceholderText(container, text, ...rest) { | |
const els = queryAllByPlaceholderText(container, text, ...rest) | ||
if (!els.length) { | ||
throw new Error( | ||
`Unable to find an element with the placeholder text of: ${text} \n\n${debugDOM( | ||
`Unable to find an element with the placeholder text of: ${text} ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
|
@@ -206,13 +216,13 @@ function getAllByLabelText(container, text, ...rest) { | |
const labels = queryAllLabelsByText(container, text, ...rest) | ||
if (labels.length) { | ||
throw new Error( | ||
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly. \n\n${debugDOM( | ||
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly. ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
} else { | ||
throw new Error( | ||
`Unable to find a label with the text of: ${text} \n\n${debugDOM( | ||
`Unable to find a label with the text of: ${text} ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
|
@@ -229,7 +239,7 @@ function getAllByText(container, text, ...rest) { | |
const els = queryAllByText(container, text, ...rest) | ||
if (!els.length) { | ||
throw new Error( | ||
`Unable to find an element with the text: ${text}. 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. \n\n${debugDOM( | ||
`Unable to find an element with the text: ${text}. 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. ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
|
@@ -245,7 +255,7 @@ function getAllByAltText(container, alt, ...rest) { | |
const els = queryAllByAltText(container, alt, ...rest) | ||
if (!els.length) { | ||
throw new Error( | ||
`Unable to find an element with the alt text: ${alt} \n\n${debugDOM( | ||
`Unable to find an element with the alt text: ${alt} ${debugDOM( | ||
container, | ||
)}`, | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we could probably change this to: