diff --git a/src/__tests__/helpers.js b/src/__tests__/helpers.js index 717fd776..83d45052 100644 --- a/src/__tests__/helpers.js +++ b/src/__tests__/helpers.js @@ -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(() => diff --git a/src/helpers.js b/src/helpers.js index 64031562..567f2791 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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 @@ -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`, )