Skip to content

Commit

Permalink
feat: Add extra error messages when screen was passed instead of a …
Browse files Browse the repository at this point in the history
…DOM element (#949)

Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
Co-authored-by: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com>
  • Loading branch information
3 people authored May 10, 2021
1 parent 4b2976d commit c273ed5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {screen} from '../'
import {
getDocument,
getWindowFromNode,
Expand All @@ -10,6 +11,13 @@ test('returns global document if exists', () => {
})

describe('window retrieval throws when given something other than a node', () => {
// we had an issue when user insert screen instead of query
// actually here should be another more clear error output
test('screen as node', () => {
expect(() => getWindowFromNode(screen)).toThrowErrorMatchingInlineSnapshot(
`"It looks like you passed a \`screen\` object. Did you do something like \`fireEvent.click(screen, ...\` when you meant to use a query, e.g. \`fireEvent.click(screen.getBy..., \`?"`,
)
})
test('Promise as node', () => {
expect(() =>
getWindowFromNode(new Promise(jest.fn())),
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 (
typeof node.debug === 'function' &&
typeof node.logTestingPlaygroundURL === 'function'
) {
throw new Error(
`It looks like you passed a \`screen\` object. Did you do something like \`fireEvent.click(screen, ...\` when you meant to use a query, e.g. \`fireEvent.click(screen.getBy..., \`?`,
)
} else {
// The user passed something unusual to a calling function
throw new Error(
Expand Down

0 comments on commit c273ed5

Please sign in to comment.