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
802 stars 105 forks source link

Add access to original Date object #239

Closed chrisworfolksky closed 5 years ago

chrisworfolksky commented 5 years ago

I am using sinon.useFakeTimers to mock the date for some unit tests. This is working fine. However, I've also got some debugging logs and I want to add a timestamp to each line. However, as I am using the clock, I get the mocked date in my timestamps, rather than the actual date.

I don't want to call the uninstall method because I still want it installed for the rest of the test.

One potential solution would be if we could call the original Date object manually, something like:

clock.getOriginalDate()
benjamingr commented 5 years ago

Thanks for opening an issue!

You can always get non mocked instances by creating a new realm and "stealing" date from there or getting a reference to Date before clock.install

var originalDate = Date;
clock.install();

new originalDate(); // correct
chrisworfolksky commented 5 years ago

Thanks!