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.

Clear all timers without using fake timers

See original GitHub issue

🚀 Feature Proposal

Make jest.clearAllTimers (and maybe other timer methods) to work without jest.useFakeTimers().

Or allow an option for jest.useFakeTimers(), which would not freeze the timers, but allow the timers methods to be executed at the same time.

Motivation

We are running a little bit more complex integration tests with jest. We setup our whole web application into jsdom, then perform some simple actions and evaluate the results. The problem is, that our application also runs some intervals and timeouts. Some of them may run forever (i.e. refresh auth token), some other may end after a few seconds, or minutes (i.e. some prefetch actions), but we do not care about them in the specified test so they can be canceled. We are not able to access these intervals, or timeouts (except for mocking the setTimeout/setInterval methods and implementing this feature ourselves), since the test actually sets up whole application, not just a part of it. We are also not able to use the jest.useFakeTimers(), since we need to run some timers as they are supposed to during the booting state of the application. We would like to run jest.clearAllTimers in the afterEach script without actually using the fake timers, since we need to run real timers in other cases.

Example

// Maybe this method could wrap the actual timer functions
jest.useRealTimers();

afterEach(() => {
  // This would clear all existing timer functions
  jest.clearAllTimers();
});

it('can run complex application and clear all timers after the test is done', () => {
  // This sets up application with all timers, which may run for a long time, or forever
  let app = setupApplcation();

  // Check that the application is fully loaded
  expect(app.state).toEqual('loaded');
});

Pitch

This would extend jest-fake-timers module with useful features for more complex scenarios.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
Filipolikocommented, Jan 9, 2020

Hi, thanks for your hint. I checked out Lolex and it seems like an overkill. It also affects the behaviour of the timers (by advancing time every X ms by X ms), which probably won’t cause any unexpected behaviour, but still, it seems unnecessary. I would rather have jest.useFakeTimers({ advanceTime: true }), which runs all timers as they are supposed to, while allowing to call fake timer methods, with exception of methods affecting the timers times (since it would probably be a more complex implementation).

For now, I implemented simple solution with wrappers around timer methods for our project use case, but it would be great to be able to do this via jest native methods.

0reactions
github-actions[bot]commented, May 4, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timer Mocks - Jest
In the following example we enable fake timers by calling jest. ... be useful in some tests to be able to clear all...
Read more >
Jest issue. FakeTimers: clearTimeout was invoked to clear a ...
Seems like the problem in timers, cause the first error message says: "FakeTimers: clearTimeout was invoked to clear a native timer instead of ......
Read more >
Using Fake Timers | Testing Library
This will ensure you flush all the pending timers before you switch to real timers. If you don't progress the timers and just...
Read more >
Timer Mocks - Jest - w3resource
In this case we enable fake timers by calling jest.useFakeTimers();. This will mock out setTimeout and other timer functions using mock ...
Read more >
Fake timers - Sinon.JS
Fake timers are synchronous implementations of setTimeout and friends thatSinon.JS can overwrite the global functions with to allow you to more easilytest ...
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