Jasmine date mocking with moment.js
Check out how moment mock dates themselves in their own test suite: https://github.com/moment/moment/blob/2e2a5b35439665d4b0200143d808a7c26d6cd30f/src/test/moment/now.js#L15
jasmine.clock().mockDate
expects Date
as input. Date
and moment
are not fully compatible. If you provide the to-be-mocked date in the spec itself you could simply use Date
there instead.
If your code generates a moment you want to mock, or you'd rather use the moment API, take a look at moment.toDate()
. This method returns the Date
object backing a moment.
it('uses the mocked time with moment', function() {
var today = moment('2015-10-19').toDate();
jasmine.clock().mockDate(today);
expect(moment().valueOf()).toEqual(today.valueOf());
});