Posted on 2015-03-19
When you have time-sensitive operations that require using the current date, it’s always a mess to test.
new Date() or
new Date(Date.now()). But if we start using that everywhere, when
we get to a point where we want to test operations that lookup the current date, we become a bit stuck. Since our test data would be static, but not
the current date.
Therefore, in our application, we simply created a global method called
getCurrentDate which, by default, returns
beforeEach setup of the test.
With that, our current date is always what we want in tests. We’ve also used it in our Selenium tests to ensure our UI thinks it’s the date we want.
It requires making changes to an existing app if you are already using
new Date() everywhere. It’s a small cost to pay for testability!