更新:如果您想动态更改 TZ,这不会产生确定性结果,但如果您只想要一个 TZ,它会起作用。它可能是在脚本级别指定的替代方法,但我认为这将是更好的答案。
问题在于,通过process.env.TZ
在运行时设置,它会在常规 Jest 测试运行期间跨测试产生不确定性行为(副作用)。如果您使用which 串行运行测试,它可能会起作用--runInBand
,但我不会指望它。
我还发现了一个关于 Node 中动态时区的旧存档问题,看起来动态调整它通常不起作用。
相反,我可能最终会得到多个脚本,每个脚本都TZ
在启动之前设置jest
。
对于我的用例,我实际上想在不同时区下针对特定的基于日期的边缘情况运行测试。有时用户会遇到基于时区的错误,我们希望在我们的测试套件中轻松覆盖。
我们默认使用此处建议的答案之一运行项目中的所有测试,通过TZ=UTC
在npm
脚本中设置(例如TZ=UTC npm run jest
。这将在 UTC 时区下运行所有测试。
然后,我们利用testEnvironment
可以使用 JSDoc pragma在测试套件级别设置的配置@jest-environment
。使用此自定义测试环境,然后我们可以使用“自定义文档块编译指示”(如@timezone
. 这使每个测试套件的时区自定义成为可能,这不像每个测试那样理想,但足以满足我们的目的。
jsdom-with-timezone.js
const JSDOMEnvironment = require('jest-environment-jsdom');
/**
* Timezone-aware jsdom Jest environment. Supports `@timezone` JSDoc
* pragma within test suites to set timezone.
*
* You'd make another copy of this extending the Node environment,
* if needed for Node server environment-based tests.
*/
module.exports = class TimezoneAwareJSDOMEnvironment extends JSDOMEnvironment
{
constructor(config, context) {
// Allow test suites to change timezone, even if TZ is passed in a script.
// Falls back to existing TZ environment variable or UTC if no timezone is specified.
// IMPORTANT: This must happen before super(config) is called, otherwise
// it doesn't work.
process.env.TZ = context.docblockPragmas.timezone || process.env.TZ || 'UTC';
super(config);
}
};
tz-eastern.test.js
/**
* @timezone America/New_York
*/
describe('timezone: eastern', () => {
it('should be America/New_York timezone', () => {
expect(process.env.TZ).toBe('America/New_York');
expect(new Date().getTimezoneOffset()).toBe(300 /* 5 hours */);
});
});
开玩笑的配置文件
module.exports = {
"testEnvironment": "<rootDir>/jsdom-with-timezone.js"
}
使用它jest.useFakeTimers('modern');
并jest.setSystemTime()
足以进行更强大的日期测试,所以我想我会分享这种方法让其他人从中受益!由于 pragma 处理是自定义的,您可以为您的用例以任何您喜欢的方式自定义它。
资料来源: