Mocking dates in AngularJS / Jasmine tests

Jasmine (2.2) Clock can mock dates and time.

http://jasmine.github.io/2.2/introduction.html#section-Mocking_the_Date

For example (from the docs):

it("mocks the Date object and sets it to a given time", function() {
  var baseTime = new Date(2013, 9, 23);
  jasmine.clock().mockDate(baseTime);

  jasmine.clock().tick(50);
  expect(new Date().getTime()).toEqual(baseTime.getTime() + 50);
});

Leave a Comment

tech