`clearTimeout` will not clear timers created prior to clock installation
See original GitHub issueWe understand you have a problem and are in a hurry, but please provide us with some info to make it much more likely for your issue to be understood, worked on and resolved quickly.
- FakeTimers version : 8.0.1
- Environment : node12, macOS Catalina 10.15.7
- Other libraries you are using: several, this is happening in tests for a VS Code extension
What did you expect to happen?
clearTimeout
to clear timers even if they weren’t created with the mock.
What actually happens The timer that should have been cleared still fires.
How to reproduce
Describe with code how to reproduce the faulty behaviour or link to code on JSBin or similar
const timer = setTimeout(() => console.log('This should not be called'), 1);
const clock = FakeTimers.install();
clearTimeout(timer);
clock.uninstall();
// ... wait 1 ms + next event loop
// > 'This should not be called'
I can make a PR to fix this if the current behavior is not intended. I added a patch to my own source, though a patch here would be much more robust (need to consider things like duplicate handles, clearInterval
, clearImmediate
, etc.)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:12 (10 by maintainers)
Top Results From Across the Web
timer created with setInterval() but cleared with clearTimeout ...
Because clearTimeout() and clearInterval() clear entries from the same list, either method can be used to clear timers created by ...
Read more >setTimeout / clearTimeout problems - javascript - Stack Overflow
The timer starts fine, but is not reset on a click. If the function is called 5 times within the first 10 seconds,...
Read more >JavaScript setTimeout() – How to Set a Timer in JavaScript or ...
To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the ......
Read more >Scheduling: setTimeout and setInterval
To cancel the execution, we should call clearTimeout/clearInterval with the value returned by setTimeout/setInterval .
Read more >Timer Mocks - Jest
The native timer functions (i.e., setTimeout(), setInterval(), clearTimeout(), clearInterval()) are less than ideal for a testing ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I made this issue more as a feature request rather than necessarily a bug report. I think respecting the expectations of older timers is useful functionality, though it might not quite fit within this library. I was leaning more towards an opt-in flag to enable this behavior rather than it be the default. Also, a warning at the very least serves to make debugging less obnoxious, otherwise things can randomly break with no clear reason why (this was the most frustrating part).
Just a little more about the context in which I’m using Sinon in:
I work on an extension for VS Code where many of our ‘unit tests’ are more like mini-integration tests. It is cumbersome to stub/mock every dependency since we essentially live inside our dependency. So these APIs still run in the background, occasionally breaking during tests because of
clearTimeout
being replaced. There’s ways around this, but for us the cleanest way is to preserveclearTimeout
behavior for pre-existing timers. In truth our tests should’ve been written with more separation in mind, but that was before my time.Closed by #407