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:
- Created 4 years ago
- Reactions:2
- Comments:5
Top GitHub Comments
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.
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.