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.

getDocumentElement null during testing

See original GitHub issue

I’m using https://www.radix-ui.com/docs/primitives/components/dropdown-menu in our design system which ends up using floating-ui under the hood. Everything is working fine except in jest when I click on the trigger and the popover is supposed to appear I get the following error from floating-ui/dom:

/Users/raza.jamil/dev/reckon-frontend/node_modules/.pnpm/@floating-ui/dom@0.5.4/node_modules/@floating-ui/dom/dist/floating-ui.dom.umd.js:133
    return ((isNode(node) ? node.ownerDocument : node.document) || window.document).documentElement;
                                                                                    ^

TypeError: Cannot read properties of null (reading 'documentElement')
    at getDocumentElement (/Users/raza.jamil/dev/reckon-frontend/node_modules/.pnpm/@floating-ui/dom@0.5.4/node_modules/@floating-ui/dom/dist/floating-ui.dom.umd.js:133:85)
    at Object.convertOffsetParentRelativeRectToViewportRelativeRect (/Users/raza.jamil/dev/reckon-frontend/node_modules/.pnpm/@floating-ui/dom@0.5.4/node_modules/@floating-ui/dom/dist/floating-ui.dom.umd.js:277:29)
    at detectOverflow (/Users/raza.jamil/dev/reckon-frontend/node_modules/.pnpm/@floating-ui/core@0.7.3/node_modules/@floating-ui/core/dist/floating-ui.core.umd.js:272:128)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at fn (/Users/raza.jamil/dev/reckon-frontend/node_modules/.pnpm/@floating-ui/core@0.7.3/node_modules/@floating-ui/core/dist/floating-ui.core.umd.js:825:26)
    at Object.computePosition (/Users/raza.jamil/dev/reckon-frontend/node_modules/.pnpm/@floating-ui/core@0.7.3/node_modules/@floating-ui/core/dist/floating-ui.core.umd.js:141:11)

I’ve narrowed down the issue to this function https://github.com/floating-ui/floating-ui/blob/master/packages/dom/src/utils/getDocumentElement.ts.

It seems that (isNode(node) ? node.ownerDocument : node.document) || window.document) can be null causing the error. If I just use optional chaining and change it to (isNode(node) ? node.ownerDocument : node.document) || window.document)?. documentElement , the test starts working.

There’s some issue with the Codesandbox template provided, I can’t seem to run tests in it. I’ll work on it but in the meantime does this issue and the fix make sense or am I missing something?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
atomikscommented, Nov 3, 2022

OK so running some tests locally, it seems like Jest 27-29 doesn’t have the issue (with jest-environment-jsdom@27-29)

For Jest 26 it looks like it’s this issue: https://github.com/facebook/jest/issues/8197#issuecomment-611880541

To fix it make sure you flush the microtask queue before the test finishes, this fixed it for me locally:

https://floating-ui.com/docs/react-dom#testing

Screenshot 2022-11-03 at 12 41 58 pm
0reactions
mkrdscommented, Nov 3, 2022

Hey @atomiks thanks for the reply. Yes updating Jest to 27^ fixes this issue. I’m a little bit hesitant about that as I’m one of the maintainers of a component library used by multiple projects in Sage organization and I’m afraid we might get swamped with GH issues from angry people confused why their test suites are failing with a slightly confusing error message. On the other hand Jest@26 is already 2 years so yeah, maybe it’s finally time for an upgrade

Read more comments on GitHub >

github_iconTop Results From Across the Web

document.getDocumentElement returns null — oracle-tech
I tested it and found out, that the problem is the Default Java XML Parser. ... XP/SP2 or Linux (SuSE 9.0) I always...
Read more >
Why would a DOM Document show as null when it is actually ...
Document instantiated and seems to be initialized properly since when I dump it using: LOG.info("DOM: " + doc.getDocumentElement().
Read more >
JavaScript method: XML.getDocumentElement()
This method returns null if applied to an empty or invalid XML object. Example. This example displays the XML document element from four...
Read more >
getBaseURI() returns null when base URI is defined
getBaseURI() returns null in case when the base URI is defined by the attribute 'xml:base' in a root element (see test.java below). According...
Read more >
xml/src/test/java/tests/api/javax/xml/parsers ...
you may not use this file except in compliance with the License. * You may obtain a copy of the License ... Case...
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