Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

thbar opened this Issue Aug 15, 2012 · 10 comments


None yet
4 participants

thbar commented Aug 15, 2012

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.

thbar commented Aug 15, 2012

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

Updated fiddle: http://jsfiddle.net/thbar/fA3kv/2/

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.

thbar commented Aug 15, 2012

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

micho commented Jul 22, 2013

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

thbar commented Jul 23, 2013

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.

thbar commented Jul 23, 2013

Updated fiddle:


(using rawgithub.com instead of raw.github.com, and 1.7.3 instead of 1.4.2).

thbar commented May 21, 2014

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).

thbar commented May 21, 2014

@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 May 21, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment