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 :
- You can use moment-timezone to mock timezones in Jasmine which I unfortunately did not manage to use
- How to mock the browser’s timezone? which seemed a bit of hack
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 :
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.
Leave a comment