Skip to content

Commit

Permalink
fix(error-messages): issue: Output User Error instead of New Issue er…
Browse files Browse the repository at this point in the history
…ror for wrong node from user

 - by adding additional check and correct error-message according to edge use case from the issue testing-library#911
  • Loading branch information
savcni01 committed May 5, 2021
1 parent c50c382 commit cf822ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/__tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('window retrieval throws when given something other than a node', () =>
// actually here should be another more clear error output
test('screen as node', () => {
expect(() => getWindowFromNode(screen)).toThrowErrorMatchingInlineSnapshot(
`"Unable to find the \\"window\\" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new"`,
`"It looks like you used 'screen' object instead of one of selectors from screen.selectMethod and it passed a 'screen' instead of a DOM node. Did you do something like \`fireEvent.click(screen, ...\` when you meant to use a \`fireEvent.click(screen.getBy..., \`?"`,
)
})
test('Promise as node', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ function getWindowFromNode(node) {
throw new Error(
`It looks like you passed an Array instead of a DOM node. Did you do something like \`fireEvent.click(screen.getAllBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`?`,
)
} else if (
node.debug instanceof Function &&
node.logTestingPlaygroundURL instanceof Function
) {
throw new Error(
`It looks like you used 'screen' object instead of one of selectors from screen.selectMethod and it passed a 'screen' instead of a DOM node. Did you do something like \`fireEvent.click(screen, ...\` when you meant to use a \`fireEvent.click(screen.getBy..., \`?`,
)
} else {
// The user passed something unusual to a calling function
throw new Error(
Expand Down

0 comments on commit cf822ca

Please sign in to comment.