Skip to content

waitForElementToBeRemoved can time out even when the element has been removed from the document #1204

Open
@ericbiewener

Description

@ericbiewener

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 returns true.
  • However, while debugging, I found that document.contains(element) and document.contains(parent) both return false.
  • In other words, parent has been removed from the document, but dom-testing-library doesn't check against that.
  • When this occurs, I can see that parent is of type HTMLDivElement. I would have expected it to be HTMLHtmlElement because of this while 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 the while 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).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions