Skip to content
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

Added explicit error message for Promises passed to getWindowFromNode #646

Merged
merged 2 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import {getDocument, checkContainerType} from '../helpers'
import {getDocument, getWindowFromNode, checkContainerType} from '../helpers'

test('returns global document if exists', () => {
expect(getDocument()).toBe(document)
})

describe('window retrieval throws when given something other than a node', () => {
test('Promise as node', () => {
expect(() =>
getWindowFromNode(new Promise(jest.fn())),
).toThrowErrorMatchingInlineSnapshot(
`"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 do \`fireEvent.click(await 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"`,
)
})
})

describe('query container validation throws when validation fails', () => {
test('undefined as container', () => {
expect(() =>
Expand Down
7 changes: 5 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function getDocument() {
return window.document
}
function getWindowFromNode(node) {
// istanbul ignore next I'm not sure what could cause the final else so we'll leave it uncovered.
if (node.defaultView) {
// node is document
return node.defaultView
Expand All @@ -57,8 +56,12 @@ function getWindowFromNode(node) {
} else if (node.window) {
// node is window
return node.window
} else if (node.then instanceof Function) {
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 do \`fireEvent.click(await screen.getBy...\`?`,
)
} else {
// no idea...
// The user passed something unusual to a calling function
throw new Error(
`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