Date() is different when run local compared to in pipeline

Viewed 386

I am doing snapshot tests with Jest and during one of those tests i am creating a new Date using new Date() the problem is when i run the tests locally on my computer the date is in german: Mon Jan 20 2020 01:00:00 GMT+0100 (Mitteleuropäische Normalzeit) and when i run the same test with my gitlab pipeline the text is in english Mon Jan 20 2020 00:00:00 GMT+0000 (Coordinated Universal Time). This results in my pipeline failing. Is there an option for globally setting my language for jest. I was able to set the timezone using process.env.TZ = 'UTC'; is there something similar for the language?

1 Answers

To prevent problems related to timezones (e.g. date formating), you can set node timezone in the jest config file. Now you are sure all tests are executed at the same timezone no matter where your colleagues or your CI server are.

Please, adjust your jest config file:

// jest.config.js
process.env.TZ = 'GMT';

module.exports = {
  // ...
}

Resource - https://dev.to/maxpou/how-to-mock-date-with-jest-3k4b.

Related