How to Mock Your Browser's Timezone With Jasmine and MomentJS

Last week, I’ve been working at adding a distributed countdown to my Online Planning Poker App. As our team works from Paris and Beirut, I wanted to unit test that it would work well through different timezones. I found a surprisingly simple solution.

What Google told me

I first searched Google to see how to do it. I found 2 answers that looked promising :

Known results for such a simple situation were disappointing !

What I ended up with

After a good deal of dabbling around, I eventually found a pretty simple solution using Jasmine and Moment Timezone :

1
2
3
jasmine.clock().install();
...
jasmine.clock().mockDate(moment.tz("2017-03-23 10:00:00", "Europe/Paris").toDate())

Obviously, the drawback is that it implies setting both the timezone and the time. This should be ok in most of unit tests though, but might be an issue in some cases.

Comments