admin管理员组

文章数量:1296490

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 puter 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?

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 puter 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?

Share asked Oct 27, 2021 at 12:01 timmmmmbtimmmmmb 79611 silver badges27 bronze badges 4
  • 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 – Metro Smurf Commented Oct 27, 2021 at 12:09
  • does this help? stackoverflow./questions/15141762/… But I would consider refactor the test itself to make it not depend on timezone. – spiritwalker Commented Oct 27, 2021 at 12:09
  • Can you use this answer: reactjs - How do I set a timezone in my Jest config? - Stack Overflow – chrwahl Commented Oct 27, 2021 at 12:27
  • Does this answer your question? get UTC date (not UTC string) in javascript – Michael Freidgeim Commented Jan 9, 2022 at 3:10
Add a ment  | 

1 Answer 1

Reset to default 11

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.

本文标签: javascriptDate() is different when run local compared to in pipelineStack Overflow