Skip to content

Commit

Permalink
feat: Improve error message when passing an Array while a single Elem…
Browse files Browse the repository at this point in the history
…ent is expected (#906)
  • Loading branch information
juanca authored Mar 7, 2021
1 parent f7b5c33 commit 300bfe2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/__tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ describe('window retrieval throws when given something other than a node', () =>
`"It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?"`,
)
})
test('Array as node', () => {
expect(() => getWindowFromNode([])).toThrowErrorMatchingInlineSnapshot(
`"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...\`?"`,
)
})
test('unknown as node', () => {
expect(() => getWindowFromNode({})).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"`,
Expand Down
4 changes: 4 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ function getWindowFromNode(node) {
throw new Error(
`It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?`,
)
} else if (Array.isArray(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 {
// The user passed something unusual to a calling function
throw new Error(
Expand Down

0 comments on commit 300bfe2

Please sign in to comment.