question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

infinite loop when DOM mutation happens in waitFor callback

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:18 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
ryuuji3commented, Nov 21, 2020

The very first step of waitFor with or without fake timers is to check the callback. (https://github.com/testing-library/dom-testing-library/blob/master/src/wait-for.js#L56 or https://github.com/testing-library/dom-testing-library/blob/master/src/wait-for.js#L96) waitFor doesn’t consider booleans, its either callback throws (try again) or doesn’t throw (success). (https://github.com/testing-library/dom-testing-library/blob/master/src/wait-for.js#L146) If you need to verify against boolean then your callback should use toBeTruthy() eg.

await waitFor(() => expect(condition).toBeTruthy())
1reaction
SerkanSipahicommented, Nov 17, 2020

I do not really understand how await waitFor(() => false); console.log('Foo') work. In my case waitFor waits for nothing. No matter if the result of the callback is true or false, the promise (waitFor) will always be resolved and Foo is logged. Have I understood something wrong here?

some env infos:

  • real browser with karma + jasmine.
  • angular extension of the testing-library. – waitFor is re-exported from testing-library. Therefore the issue should be reported here 👍
Read more comments on GitHub >

github_iconTop Results From Across the Web

Common mistakes with React Testing Library - Kent C. Dodds
Performing side-effects in waitFor​​ Because of this, the callback can be called (or checked for errors) a non-deterministic number of times and ...
Read more >
Infinite loop in useEffect - reactjs - Stack Overflow
Passing an empty array as the second argument to useEffect makes it only run on mount and unmount, thus stopping any infinite loops....
Read more >
Async Methods - Testing Library
waitFor may run the callback a number of times until the timeout is reached. Note that the number of calls is constrained by...
Read more >
testing library: side-effects and `waitfor` - code-comments
When any of those changes occur, it will re-run the callback. I believe this is where I ran into trouble and created a...
Read more >
Getting To Know The MutationObserver API
Monitoring for changes to the DOM is sometimes needed in complex web ... let observer = new MutationObserver(callback); function callback ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found