简体   繁体   English

Date() 在本地运行时与在管道中运行时不同

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

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).我正在使用 Jest 进行快照测试,在其中一个测试期间,我正在使用 new Date() 创建一个新日期,问题是当我在计算机上本地运行测试时,日期为德语:2020 年 1 月 20 日星期一 01:00: 00 GMT+0100(Mitteleuropäische Normalzeit),当我使用我的 gitlab 管道运行相同的测试时,文本为英文 Mon Jan 20 2020 00:00:00 GMT+0000(协调世界时)。 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';我能够使用process.env.TZ = 'UTC';设置时区is there something similar for the language?语言有类似的东西吗?

To prevent problems related to timezones (eg date formating), you can set node timezone in jest config file.为了防止与时区相关的问题(例如日期格式),您可以在 jest 配置文件中设置节点时区。 Now you are sure all tests are executed on the same timezone no matter where your colleagues or your CI server are.现在,无论您的同事或 CI 服务器在哪里,您都可以确定所有测试都在同一个时区执行。 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 .资源 - https://dev.to/maxpou/how-to-mock-date-with-jest-3k4b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与本地主机相比,部署的响应式 css 看起来不同 - Deployed responsive css looks different compared to local host Javascript:在计算机和本地服务器上运行时的行为不同 - Javascript : Different behavior when run on machine and local server 时间戳在本地机器和 gitlab 管道上的解释不同 - Timestamp interpreted different on local machine and on gitlab pipeline 使用getTimezoneOffset将UTC日期时间转换为本地日期时间时,在IE和Chrome中获得不同的结果 - Getting different result in IE and Chrome when converting UTC date time into local date time using getTimezoneOffset 与 js bin 相比,Mocha 测试返回不同的答案 - Mocha test returning different answer when compared to js bin JavaScript 数学计算 - 与 Excel 公式相比的不同结果 - JavaScript Mathematical calculation - Different results when compared to Excel Formula 与 JavaScript 相比,从 HTML 设置属性时的不同行为 - Different behavior when setting attribute from HTML compared to JavaScript 单击范围元素与拖动元素相比的不同步骤 - Different steps when clicking range elements compared to dragging them 与 Date() 相比,为什么使用 Date.UTC() 返回完全不同的月份值? - Why does using Date.UTC() return a completely different month value compared to Date()? 转换后的日期不同 - Date is different when converted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM