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
804 stars 106 forks source link

Add mocks to NodeJS "timers" module #467

Closed swenzel-arc closed 1 year ago

swenzel-arc commented 1 year ago

Purpose (TL;DR) - mandatory

Fix issue #466

Solution

It works like this:

Try to import timers module as timersModule. If it doesn't work, that's okay.

Upon FakeTimers.install(), iff we're installing on the globalObject and timersModule was successfully imported: For each property that was mocked on the globalObject iff it also exists on timersModule: Safe its name and original value from timersModule to a new property on the Clock object. Then set it to the same property as the faked property on globalObject.

Upon FakeTimers.uninstall(), iff the object holding timersModule's originals is truthy: For each entry set the corresponding property back to the original.

swenzel-arc commented 1 year ago

All done :)
Btw. do you think we should add a config flag that allows to skip altering the timers module?

benjamingr commented 1 year ago

Btw. do you think we should add a config flag that allows to skip altering the timers module?

No but that's a good point, it should respect the regular "should I mock setTimeout/setInterval or whatever" option (toFake)

swenzel-arc commented 1 year ago

Does it correctly not mock methods if they're not part of toFake?

Yes it does :)

fatso83 commented 1 year ago

Out as 10.2.0

fatso83 commented 1 year ago

Seems like #470 made a good candidate for why this could have been behind a flag. Some weird regression hitting us.