Angular Unit Test - how simulate system clock timezone
Asked Answered
C

0

10

How is it possible to simulate the system clock being set to another timezone in an Angular Unit Test with Karma/Jasmine.

I am working an a custom datetime picker component. It has a bug that can only be reproduced when using it in a different timezone. To write a proper unit test for the fix I want to simulate the exact same situation.

I can reproduce the bug by switching the system clock manually in the windows settings:

E.g. enter image description here

Now I want to move the system time switch to the unit test to simulate the behavior of the component in different timezones.

I tried to use jasmine.clock like this: jasmine.clock().mockDate(moment.tz("2020-10-09 10:00:00", "Asia/Bangkok").toDate());

But it does not really change the timezone like changing the system clock does. Jasmine clock changes indeed the time, but it stays in the timezone of the system (which is Central European Time).

Cheerly answered 21/10, 2020 at 7:30 Comment(1)
I did not follow this up anymore: but this looks promising: npmjs.com/package/mockdate You can set what new Date() will return.Cheerly

© 2022 - 2024 — McMap. All rights reserved.