Skip to content

Commit 300bfe2

Browse files
authored
feat: Improve error message when passing an Array while a single Element is expected (#906)
1 parent f7b5c33 commit 300bfe2

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/__tests__/helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ describe('window retrieval throws when given something other than a node', () =>
1717
`"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...\`?"`,
1818
)
1919
})
20+
test('Array as node', () => {
21+
expect(() => getWindowFromNode([])).toThrowErrorMatchingInlineSnapshot(
22+
`"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...\`?"`,
23+
)
24+
})
2025
test('unknown as node', () => {
2126
expect(() => getWindowFromNode({})).toThrowErrorMatchingInlineSnapshot(
2227
`"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"`,

src/helpers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ function getWindowFromNode(node) {
9292
throw new Error(
9393
`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...\`?`,
9494
)
95+
} else if (Array.isArray(node)) {
96+
throw new Error(
97+
`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...\`?`,
98+
)
9599
} else {
96100
// The user passed something unusual to a calling function
97101
throw new Error(

0 commit comments

Comments
 (0)