sinonjs / fake-timers

Fake setTimeout and friends (collectively known as "timers"). Useful in your JavaScript tests. Extracted from Sinon.JS
BSD 3-Clause "New" or "Revised" License
793 stars 103 forks source link

feat: warpFactor #460

Closed Noriller closed 4 months ago

Noriller commented 1 year ago

Purpose (TL;DR) - mandatory

Adding feature: warpFactor that changes the normal delay/timeout by the factor provided. Issues: #459 #16

This is a dependency of Jest that is widely used. In frontend testing, many component libraries abstract micro animations that take time to complete and slow down testing.

Using fake timers would mean having to spam methods to run the timers, which is not optimal.

This PR aims to add a way to speed up all those hidden timers without anything more than passing a flag with the factor to be used.

Mainly, I added the warpFactor to the config and when present, it takes the delay and divides it by the factor.

The one thing I want to highlight is that if the factor is higher than the timeout, then the timeouts are rounded to zero.

This would throw off the ordering of timers, since you can possibly make all timers 0. So I added something to keep track of what would be the actual callAt to make use in the compare timers function.

I'm not sure in what scenario it could be useful, but this implementation also lets you use an fraction as the warpFactor, making all timeouts run slower than normal.

Noriller commented 1 year ago

At the time I didn't think of that. I've started from 0 with the new set of changes.

codecov[bot] commented 1 year ago

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (ad1d43c) 95.52% compared to head (b7cbb33) 95.14%. Report is 47 commits behind head on main.

Files Patch % Lines
src/fake-timers-src.js 78.57% 3 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #460 +/- ## ========================================== - Coverage 95.52% 95.14% -0.39% ========================================== Files 2 2 Lines 648 659 +11 ========================================== + Hits 619 627 +8 - Misses 29 32 +3 ``` | [Flag](https://app.codecov.io/gh/sinonjs/fake-timers/pull/460/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) | Coverage Δ | | |---|---|---| | [unit](https://app.codecov.io/gh/sinonjs/fake-timers/pull/460/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) | `95.14% <78.57%> (-0.39%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

fatso83 commented 1 year ago

Yeah, as Simen asked (to be answered) in the original issue, I also don't understand the usecase or what isn't handled by .runAll() or tick(n*1000).

So an elaboration in the original issue would make understanding the WHY a bit smoother 🙂 Minimal API surface is a quality.

stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

fatso83 commented 4 months ago

See https://github.com/sinonjs/fake-timers/issues/459#issuecomment-1936981297.

This is not a full stop! It can be reopened if someone provides a good usecase for why this is needed and makes sense!