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