question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to find the "window" object for the given node for input event

See original GitHub issue
  • @testing-library/dom version: 7.28.1
  • Testing Framework and version: jest 7.0.14
  • DOM Environment: jsdom 16.4.0

Relevant code or config

import React from 'react';
import { createEvent, render } from '@testing-library/react';

describe('input', () => {
  it('creates an event', () => {
    const input: HTMLInputElement = render(<input />);
    const event: React.FormEvent<HTMLInputElement> = createEvent.change(input, {})
  });
});

What you did:

I ran the above test with npm test (which runs jest).

What happened:

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

Reproduction repository: https://github.com/testing-library/dom-testing-library-template

Problem description:

Suggested solution:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mathiassoeholmcommented, Dec 29, 2020

To be clear, my suggestion is simply to change the error message from

"Unable to find the "window" object for the given node ..."

to something like

`Expected node to be a DOM Node, but got ${getTypeName(node)}`

and perhaps throw TypeError instead of Error.

The reason I mention that you’re already checking whether node is a DOM Node inside getWindowFromNode is because of:

} else if (node.ownerDocument && node.ownerDocument.defaultView) {
    // node is a DOM node
    return node.ownerDocument.defaultView

It literally says in the comment “node is a DOM node”.

I can definitely see how adding runtime type-checking to fireEvent.change itself could result in a more helpful error, but I agree on your comment about preferring static type checking and not having to maintain both.

1reaction
eps1loncommented, Dec 27, 2020

Thanks for the report.

We should check if we’re actually given an HTMLElement because that’s not the case here. What you probably wanted to do is

-const input = render(<input />);
+const { getByRole } = render(<input />);
+const input = getByRole("textbox");
const event = createEvent.change(input, {});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Error: Unable to find the "window" object for the given node
As a first question before I can give you a more accurate answer, how do you have the jest environment configured?
Read more >
Unable to find the "window" object for the given node for input ...
Unable to find the "window" object for the given node for input event.
Read more >
dom-testing-library - npm
This will search for all elements with a placeholder attribute and find one that matches the given TextMatch . // <input placeholder="Username" ...
Read more >
Window: error event - Web APIs - MDN Web Docs - Mozilla
The error event is fired on a Window object when a resource failed to load or couldn't be used — for example if...
Read more >
DOM Standard
Returns the invocation target objects of event 's path (objects on which listeners will be invoked), except for any nodes in shadow trees...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found