testing-library / testing-library/dom-testing-library
`waitForElementToBeRemoved` can time out even when the element has been removed from the `document`
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 3.3k
- Forks
- 474
- PR merge metrics
- No merged PRs in 30d
Description
@testing-library/domversion: 8.11.2- Testing Framework and version: jest@27.4.7
- DOM Environment: jsdom@16.7.0
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,
parenthas been removed from the document, butdom-testing-librarydoesn't check against that. - When this occurs, I can see that
parentis of typeHTMLDivElement. I would have expected it to beHTMLHtmlElementbecause of thiswhileloop. 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 thewhileloop?
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).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/wait-for-element-to-be-removed.js, especially the parent traversal and removal check described in the issue. Reproduce the timeout with the provided waitForElementToBeRemoved call and inspect the document and parent containment states. Done means a regression test covers a removed parent and the wait no longer times out incorrectly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100