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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to fix tests from hanging with fake modern timers #11572

Closed
meskill opened this issue Jun 15, 2021 · 5 comments
Closed

No way to fix tests from hanging with fake modern timers #11572

meskill opened this issue Jun 15, 2021 · 5 comments

Comments

@meskill
Copy link

meskill commented Jun 15, 2021

馃悰 Bug Report

Using some nodejs libraries in source code (e.g. fast-glob) and modern timers leads to hanging tests with the error Exceeded timeout of 5000 ms for a test.

I've tried different things to force timers to continue executing (e.g. jest.runAllTimers(), jest.runAllTicks()) but nothing helped.

Also changing timers type on file level (e.g. jest.useRealTimers() and jest.useFakeTimers('legacy')) doesn't prevent test from hanging - only changing timers type in jest.config.js to real or legacy fixes it.

To Reproduce

You can find reproduction steps in repo

I think issue is coming from the usage of setImmediate inside some of the dependencies, but I have no idea why it is not resolvable by any of jest's functions for timers.

Expected behavior

There should be a way to prevent the test from hanging, at least changing timers type at file level should work the same way as it working on config level.

Link to repo

https://github.com/meskill/jest27-fake-timers-hangs

envinfo

  System:
    OS: Linux 4.19 Alpine Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
  Binaries:
    Node: 14.17.0 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.13 - /usr/local/bin/npm
  npmPackages:
    jest: ^27.0.4 => 27.0.4 
@fstoerkle
Copy link

I'm facing the same issue using modern timers and cache-manager.

Here is a repo for reproducing it: https://github.com/fstoerkle/jest-modern-timers-cache-manager

@mriedem
Copy link
Contributor

mriedem commented Sep 24, 2021

Hit a similar issue after upgrading from jest 26.6.3 to 27.2.1. We have timers: fake in jest.config.js and had to change that to legacy to stop the hang. The hang appears to be in beforeEach hooks where we reset a database for the tests. I had also read https://jestjs.io/blog/2021/05/25/jest-27#flipping-defaults and saw this:

Another default that we are changing affects Fake Timers aka Timer Mocks. We introduced an opt-in "modern" implementation of Fake Timers in Jest 26 accessed transparently through the same API, but with much more comprehensive mocking, such as for Date and queueMicrotask.
This modern fake timers implementation will now be the default. If you are among the unlucky few who are affected by the subtle implementation differences too heavily to migrate, you can get back the old implementation using jest.useFakeTimers("legacy") or, if you are enabling fake timers globally via configuration, "timers": "legacy".

But didn't really appreciate what that would mean or how it breaks with existing usage of timers: fake. I guess I'm among the unlucky few that are impacted but I'm not sure why it's causing problems or how to fix it - https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers doesn't clarify that for me either. I'm also using testEnvironment: node if that matters (with node 14).

@remcohaszing
Copy link
Contributor

I ran into this issue trying to migrate from manually using @sinonjs/fake-timers to jest.useFakeTimers().

@SimenB
Copy link
Member

SimenB commented Apr 6, 2022

Fixed via #12572 by using advanceTimers: true. Available in https://github.com/facebook/jest/releases/tag/v28.0.0-alpha.8

@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

5 participants