Skip to content


Subversion checkout URL

You can clone with
Download ZIP


useFakeTimers does not seem to work with moment.js #171

thbar opened this Issue · 10 comments

4 participants


Here is a fiddle with a jasmine spec to reproduce the issue:

I'm a newcomer to Sinon.js so I could totally have overlooked something trivial, but in case: having Sinon.js to work with moment.js would be very nice.


Current fix: using moment(new Date()) instead of moment() in the code.

Updated fiddle:


This is because the iife that wraps moment.js scopes window.Date to a locally scoped variable.

    // moment.js guts

So when you override window.Date, moment still has a reference to the original window.Date.

You an either overwrite window.Date before moment executes (although you wouldn't be able to undo that), or we remove Date from the iife in moment.


Thanks for the explanation, appreciated!

Maybe having a kind of proxy earlier on which would keep a reference to the initial window.Date and then proxy or not could help.

I'll think about a solution and will share my findings here if I come up with anything, thanks!



Id vote for removing Date from the iife. Im currently writing requirejs based tests so its quite tricky to override Date just for the desired tests.

Another alternative could be to expose the internal Date reference on the moment object so we can Duck Punch it :). The iife route would be my preference though


It seems like Date has been removed from the iife, is this issue still an issue?


Yeh, this issue was resolved by the fix in #451 so can be closed Id say


I tried upgrading sinon from 1.4.2 to 1.7.3 but I still have to write moment(new Date()) instead of moment() for my spec to pass.

Will upgrade the fiddle and report back.


Updated fiddle:

(using instead of, and 1.7.3 instead of 1.4.2).


Update: I tried today with 1.10.0, the problem still occurs (not a huge deal in my case, just updating in case it helps someone else).


@gregtillbrook I misread your previous messages - sorry about that. It looks like moment.js #451 indeed solved the issue, because my problem went away when I upgraded moment.js. Closing!

@thbar thbar closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.