Open
Description
@testing-library/dom
version: 8.11.2- Testing Framework and version: [email protected]
- DOM Environment: [email protected]
Relevant code or config:
await waitForElementToBeRemoved(screen.getByText("Loading"));
What you did:
Executed test
What happened:
Test fails with error Timed out in waitForElementToBeRemoved.
Reproduction:
Not sure how to dependably repro it, but when it occurs it seems to occur deterministically.
Problem description:
- Times out because
parent.contains(element)
always returnstrue
. - However, while debugging, I found that
document.contains(element)
anddocument.contains(parent)
both return false. - In other words,
parent
has been removed from the document, butdom-testing-library
doesn't check against that. - When this occurs, I can see that
parent
is of typeHTMLDivElement
. I would have expected it to beHTMLHtmlElement
because of thiswhile
loop. Could it be that we have a race condition in which the element gets removed from the document prior to (or even during!) the execution of thewhile
loop?
Suggested solution:
Just check if document.contains(element)
rather than capturing the parent
in the first place (of course, I assume there is a reason for not taking this simple approach).