Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
thbar opened this Issue · 10 comments

4 participants

@thbar

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

http://jsfiddle.net/thbar/fA3kv/1/

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

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

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

@timrwood

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

(function(Date){
    // moment.js guts
})(Date)

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

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!

@gregtillbrook

Hi,

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

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

@gregtillbrook

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

@thbar

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

Updated fiddle:

http://jsfiddle.net/thbar/fA3kv/3/

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

@thbar

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

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