7

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?

timmmmmb
  • 674
  • 7
  • 26
  • Can you update all the tests to use UTC dates for consistency across all environments? If so, check: [get UTC date (not UTC string) in javascript](https://stackoverflow.com/questions/57810435/get-utc-date-not-utc-string-in-javascript) – Metro Smurf Oct 27 '21 at 12:09
  • does this help? https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone But I would consider refactor the test itself to make it not depend on timezone. – spiritwalker Oct 27 '21 at 12:09
  • Can you use this answer: [reactjs - How do I set a timezone in my Jest config? - Stack Overflow](https://stackoverflow.com/questions/56261381/how-do-i-set-a-timezone-in-my-jest-config) – chrwahl Oct 27 '21 at 12:27
  • Does this answer your question? [get UTC date (not UTC string) in javascript](https://stackoverflow.com/questions/57810435/get-utc-date-not-utc-string-in-javascript) – Michael Freidgeim Jan 09 '22 at 03:10

1 Answers1

8

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.

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57