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.

`clearTimeout` will not clear timers created prior to clock installation

See original GitHub issue

We 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
JadenSimoncommented, Oct 22, 2021

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 preserve clearTimeout behavior for pre-existing timers. In truth our tests should’ve been written with more separation in mind, but that was before my time.

0reactions
fatso83commented, Nov 3, 2021

Closed by #407

Read more comments on GitHub >

github_iconTop 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 >

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