2

So here is the problem. I am migrating from moment to dayjs. There is a lot of code covered with tests, where dayjs is used. Some of them I would like to make run on specific time zone.

With moment I was using moment-timezone library.

in some test data.test.ts

import moment from 'moment-timezone';
moment.tz.setDefault('Europe/Berlin');

It would affect both moment and moment.tz and moment() will show Berlin time.

With the same approach, when we use

dayjs.tz.setDefault('Europe/Berlin')

only dayjs.tz() is affected and changed to Berlin time zone, but dayjs() instance still keeps running on local time. I don't want to change the code to write with dayjs.tz(), I want to keep dayjs() logic and make tests run in timezone I specify.

Has anyone faced the same problem or knows how to solve it? I searched everywhere, but could not find a satisfying answer.

I can set process.env.TZ='SOME ZONE', but I don't want to affect the whole node enviroment.

Any clue will be much appriciated.

Tigran Petrosyan
  • 524
  • 4
  • 14

2 Answers2

0

You can have a look at here on how to change locales locally in dayjs.

beingyogi
  • 1,316
  • 2
  • 13
  • 25
0

After looking everywhere, I came to conclusion, that in the tests I need to check not a static value, but dynamic dayjs generated value. Dayjs uses Date under the hood, Date uses system time.

expect(dayjs('some_date').format('YYYY-MM-DD')).toBe('something')

This will work on local time, but for example in UTC time zone it will throw error.

What needs to be done, is just:

`expect(dayjs('some_date').format('YYYY-MM-DD')).toBe(expect(dayjs('some_date').format('YYYY-MM-DD'))`

So this way, we are checking dynamic dayjs value, which will use any time zone's time and will produce correct tests.

Tigran Petrosyan
  • 524
  • 4
  • 14