diff --git a/src/__tests__/helpers.js b/src/__tests__/helpers.js index 21f1568f..39bbe107 100644 --- a/src/__tests__/helpers.js +++ b/src/__tests__/helpers.js @@ -25,9 +25,22 @@ describe('window retrieval throws when given something other than a node', () => `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('window is not available for node', () => { + const elem = document.createElement('div') + Object.defineProperty(elem.ownerDocument, 'defaultView', { + get: function get() { + return null + }, + }) + + expect(() => getWindowFromNode(elem)).toThrowErrorMatchingInlineSnapshot( + `It looks like the window object is not available for the provided node.`, + ) + }) + 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`, + `The given node is not an Element, the node type is: object.`, ) }) }) diff --git a/src/helpers.js b/src/helpers.js index c70218cc..ed356592 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -33,6 +33,10 @@ function getWindowFromNode(node) { } else if (node.window) { // node is window return node.window + } else if (node.ownerDocument && node.ownerDocument.defaultView === null) { + throw new Error( + `It looks like the window object is not available for the provided node.`, + ) } 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 use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?`, @@ -51,7 +55,7 @@ function getWindowFromNode(node) { } else { // 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`, + `The given node is not an Element, the node type is: ${typeof node}.`, ) } }