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
getDocumentElement null during testing #1908
Comments
Strange that it would be
|
Hmm thanks, I'm having a deeper look. It's pretty strange since there are other things in the tests that use document and they seem to be working fine. |
I also have the same problem when using radix-ui. |
When you log out |
@raza-jamil-reckon @atomiks i solved it. i only just wrap Popover/Dropdown by tag div and it worked.
|
@raza-jamil-reckon @huynhthaianhhd if you can make a repro for me to investigate that would help and I can re-open if valid. As it stands, it's not actionable and I am assuming it's a testing environment or Radix UI issue, as it doesn't seem to have been reported in other component libs. Optional chaining also doesn't make sense and I'm not sure how it works because the types break with a falsy value and things expect a valid element, not |
Happening to me too, and I'm also using radix-ui, I guess their abstraction (radix-popover) is doing some magic. "jest": "29.2.1", The first error was ResizeObserver not being defined, which makes sense, but once this is fixed the next error I get after the tests pass is this one:
|
Hi, it seems like this error is happening when we face an unhandled error in the Popover, for me was an "invalid" selector that is actually valid but invalid for my Jest version, so I had to wrap that logic in a Try/catch to actually see the right error, so defensive code in floating UI would be appreciated! |
I don't understand how it occurs though, the types are valid and can't be made defensive I need a small repo to download and investigate why or how it happens, and why it only happens to Radix UI users |
Hey, unfortunately I'm having the same issue which I've encountered during a migration from popper to floating-ui, I'm not using radix btw. I've managed to reproduce this issue in stackblitz - it's a copy of the Tooltip example from docs using React@17 and Jest@26 https://stackblitz.com/edit/node-qckpvp?file=index.test.jsx |
@mkrds thanks! It seems like
Does upgrading to Jest 27-29 fix the issue locally? |
OK so running some tests locally, it seems like Jest 27-29 doesn't have the issue (with For Jest 26 it looks like it's this issue: jestjs/jest#8197 (comment) To fix it make sure you flush the microtask queue before the test finishes, this fixed it for me locally: |
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 |
I've started to get this one with |
I'm able to reproduce this error and it's due to
Node 18 to throw an error
|
To expand on this, in our case it appears that Jest was tearing down the JSDOM environment while This becomes a problem when combined with the new(ish) Node behavior of process exit upon unhandled promise rejection; our tests were passing successfully, but the Jest worker process was crashing due to these circumstances during its teardown process. Very difficult to tell that it was happening after test suites had effectively completed. A (hacky) workaround is to add an afterAll(() =>
new Promise(resolve => setTimeout(resolve, 0))
// this also works:
// new Promise(setImmediate)
); |
The error is likely occurring because you aren't waiting for the microtask queue to be flushed (and thus positioning to have occurred) in your tests, as mentioned above. This means Floating UI's positioning executes in a microtask after Jest has torn down the DOM, causing the error. In React you put |
I don't think that's quite it, at least in our case. Out of curiosity I've tried both In any event, it does seem like handling this potential promise rejection would be a good thing overall for usability within the unit-testing context, where this edge case is possible. |
Have same issue with {
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
} and:
and:
Only workaround from message of @esimons is working |
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:
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?
The text was updated successfully, but these errors were encountered: