Skip to content

waitForElement gets into infinite setTimeout loop #99

@grigasp

Description

@grigasp
  • dom-testing-library version: 3.5.1
  • node version: 8.11.2
  • npm (or yarn) version: 5.6.0

Relevant code or config:

const { waitForElement } = require("dom-testing-library");

it("shouldn't hang after succeeding", async () => {
    let didMakeMutation = false;
    const createElement = () => document.createElement("div");
    const container = createElement();
    const myCallback = () => {
        return didMakeMutation;
    };
    const result = waitForElement(myCallback, { container });
    container.appendChild(createElement());
    didMakeMutation = true;
    await new Promise((resolve) => setTimeout(resolve, 50));
});

What you did:

I'm trying to use waitForElement to wait until an element is created in the dom.

What happened:

I found that my test is passing, but the test runner (mocha) doesn't exit - it gets into infinite setTimeout loop.

Reproduction:

Repo: https://github.com/grigasp/dom-testing-library-waitforelement-issue

npm install
npm run test

Problem description:

The test succeeds, but the test runner (mocha) doesn't ever exit, because waitForElement gets into infinite setTimeout loop.

I think the problem is that waitForElement calls observer.disconnect() from the onMutation() -> onDone() callback. MutationObserver, after calling the onMutation() callback, immediately recreates the listener, and then gets into the infinite setTimeout loop.

Suggested solution:

waitForElement should somehow call disconnect() after MutationObserver recreates the listener.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions