testing-library / testing-library/dom-testing-library

infinite loop when DOM mutation happens in waitFor callback

Open
#808 18 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug documentation
Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

Description

Jest hangs on the following sample test:

const { waitFor } = require("@testing-library/dom");

describe("test", () => {
  it("test", async () => {
    let value = 1;

    setTimeout(() => {
      value = 2;
    });

    await waitFor(() => {
      // both these lines are important: it's necessary to do a mutation and to fail in the first iteration
      // It works normally, if we comment one of these lines
      document.body.setAttribute("data-something", 'whatever');
      expect(value).toEqual(2);
    });

    console.log("execution never comes here");
  });
});

Two conditions have to be met:

  • there should be a DOM mutation in waitFor callback
  • first execution of waitFor callback should fail

Expected result:
waitFor should resolve the promise after a successful iteration regardless whether there were DOM mutations or not

Current result:
waitFor calls the callback infinitely even if subsequent iterations don't throw any exception.

Environment:
@testing-library/dom@7.26.4
jest@26.6.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the waitFor entry point and reproduce the supplied Jest example with a mutation in the callback and an initially failing expectation. Trace the retry behavior and add regression coverage showing that a successful callback resolves even when it mutates the DOM.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.