Testing dates in JS using Jasmine

Reading time ~1 minute

Testing dates in unit tests is not always an easy stuff, In this post, I’m going to show an easy and elegant way to do that in JS using Jasmine.

First, take a look at the function below. It’s calculating the difference between today and a date passed by param. To make the example easier, I’m using momentJS library to work with dates.

function getDaysBetweenTodayAndADate(date) {
  let today = moment();
  return date.diff(today, 'days'); 
}

How can we test it? The moment object will always return the current date, how can we write a test that can pass every day?

In order to solve this problem, we can use Jasmine Clock to mock the current date.

describe('testing dates in js', function () {

  beforeEach(() => {
    let today = moment('2016-01-01').toDate();
    jasmine.clock().mockDate(today);
  });

  it('should return the difference between today', () => {
    let date = moment('2016-01-05');
    expect(4).toEqual(getDaysBetweenTodayAndADate(date));
  });

});

If you are using Jasmine as your test tool, it’s an easy way to mock dates!

However, if you are not using Jasmine, there is another way to solve this problem, you can create a date utility class and mock it. I usually do it using Java for example, but it’s a topic for another day.

I hope you enjoyed this post!

Until next time!

Spike and TDD - Exploring the unknown

Spikes and TDD - Exploring the unknown Continue reading

Testing Websockets with Spring

Published on May 23, 2016

Bundling Js modules with WebPack

Published on April 25, 2016