Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With modern fake timers, an infinite loop contaminates all following tests #10357

Closed
quasicomputational opened this issue Aug 2, 2020 · 4 comments

Comments

@quasicomputational
Copy link

🐛 Bug Report

Test cases that fail due to excessive timer recursion can cause failures in following tests, as the infinitely-recursing timer is not cleared between tests.

Jest version: 26.2.2

To Reproduce

jest.useFakeTimers("modern");

it("loops", () => {
    const f = () => {
        setImmediate(f);
    };
    f();
    jest.runAllTimers();
});

it("should work", () => {
    jest.runAllTimers();
});

Expected behavior

Despite the first test failing, the second ought to be green.

Observed behaviour

The second test fails due to timer recursion.

Adding clearMocks: true and resetMocks: true doesn't help matters.

@mohamedsgap
Copy link
Contributor

@SimenB is this issue relative! can i consider it as "good first issue" ? try to work on it!

@mrazauskas
Copy link
Contributor

mrazauskas commented Mar 21, 2022

The first test simply throws: 'Aborting after running 100000 timers, assuming an infinite loop!' But before throwing it still calls setImmediate(f). If I get it right, calling jest.runAllTimers() in the second test is trying to execute the scheduled call back and is hitting the same infinite loop.

All gets solved if fake timers are reset in the second test:

jest.useRealTimers().useFakeTimers("modern").runAllTimers();

Alternatively, one could use afterEach hook to clear all pending timers:

afterEach(() => {
  jest.clearAllTimers();
});

Or to reinstall fakes:

afterEach(() => {
   jest.useRealTimers();
 });

Hm.. Perhaps before throwing it is possible to clean up a timer causing infinite loop in Sinon's Fake Timers? But what if there is another timer which will cause infinite loop soon after the error? That’s complicated. Looks like it is better to clean up manually between tests.

@SimenB
Copy link
Member

SimenB commented Apr 6, 2022

I don't think we should automatically clean up timers between tests, that's up to the author

@SimenB SimenB closed this as completed Apr 6, 2022
@github-actions
Copy link

github-actions bot commented May 7, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants