jest-dom: getRootNode not returning Document for element matched with findByText query
@testing-library/jest-domversion: 5.14.1nodeversion: 14.17.1yarnversion: 1.22.10
dom-testing-libraryversion: 8.1.0@testing-library/reactversion: 12.0.0
Relevant code or config:
// [1] Sporadically fails
expect(await screen.findByText('hello')).toBeInTheDocument();
// vs.
// [2] Passes every time without fail
waitFor(() => {
expect(screen.getByText('hello')).toBeInTheDocument();
})
What you did:
Used expectation pattern [1] over [2]
What happened:
Sporadic failures
Received element is not visible (element is not in the document)
Problem description:
Essentially this check fails: https://github.com/testing-library/jest-dom/blob/main/src/to-be-in-the-document.js#L11
because sporadically getRootNode returns the React node that got matched itself rather than the Document.
Which means that findBy... succeeds in returning a matched node, however when verifying if this node is in the Document with .toBeInTheDocument() it fails.
Suggested solution:
I don’t know enough about the internals of when the root node changes from the React node to the Document node.
I know for sure the root node eventually changes to Document, hence why the waitFor [2] pattern works.
Reproducing is difficult because of it’s sporadic nature, but I’m essentially creating this issue to find out if others are being affected by this, and also to learn a bit more about the internals that could be resulting in this issue.
In the meantime I’ll be using the waitFor[2] expectation pattern from now, but I’m worried that I may forget about this issue and have sporadic failures again in the future since pattern [1] is the first that comes to mind.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 6
- Comments: 15 (2 by maintainers)
Hi, I also faced this problem and got some small reproducible patterns, and some memorization solve them.
working sample: https://codesandbox.io/s/jest-dom-404-repro-mmb2s
implementation is also folded here
It seems that the element~s~ of the first result are detached from the tree. Are you in the same situation?
I did another research for previous comment. I’ve found that the detached element is easy to get and hard to notice.
Here is my latest research. https://codesandbox.io/s/peaceful-thunder-h7tfmx
This mini-app says two points:
And we already know that components declared in render cause component switching.
So the following patterns are suspicious:
1. Inline component: Components declared in render function
Real world example: Row renderer of table-like component
full code(codesandbox)
If you like, change the version on the Dependencies tab and check it out.
It would be nice to be able to extract the variables in the parent scope, reference them using props, and move the component to the top level.
2. Shared component: Components shared by two or many parent component
This pattern also cause detached elements. Real world example is not ready, but a SPA router could belong to this pattern.
After updating our dependencies the problem we were experiencing disappeared.
Hmmm, this seems to be the key of the issue. However, we do not control
getRootNode. It’s from jsdom. I wonder if either jsdom or the wayfindByTextworks has something to do with this. I do not see much that we can do from jest-dom’s perspective.I tried this for now: I forked the codesandbox example provided by @berlysia above (see https://github.com/testing-library/jest-dom/issues/404#issuecomment-952179213). Then I focused on one of the failing tests alone, and modified it in this way:
This is the output I get in the console:
Not sure what to make of it. The element appears placed correctly in the DOM tree, but for some reason its
parentElementisnull, and calling itsgetRootElement()returns the element itself, instead of the document where it lives.I’m honestly not sure right now how
.toBeInTheDocument’s implementation is at fault here. Suggestions are welcome.