Gol*_*Jer 5 javascript testing jestjs
这个问题类似于How do I set a timezone in my Jest config? 但那里的所有答案都与*nix有关。
运行 Windows 10 时如何设置笑话测试的时区?
好的!感谢您的评论和回答。它给我指明了一个好的方向。
set-tz很酷,但cleanup()测试完成后该功能没有触发。
再加上tzutil更改 Windows 系统时间,迫使我考虑尽量减少时区更改的影响,并有办法强制恢复,而不是依赖退出侦听器。
所以,这就是它落地的地方。
// setTimezone file.
const { execSync } = require('child_process');
const os = require('os');
export const setTimezone = (TZ) => {
if (os.platform() === 'win32') {
process.env.STORED_TZ = execSync('tzutil /g').toString();
execSync(`tzutil /s "${TZ}"`);
console.warn(`Windows timezone changed from "${process.env.STORED_TZ}" to "${TZ}"\nRun \`tzutil /s "${process.env.STORED_TZ}"\` to set it back manually.`);
} else {
process.env.TZ = TZ;
}
};
export const restoreTimezone = () => {
if (os.platform() === 'win32') {
execSync(`tzutil /s "${process.env.STORED_TZ}"`);
console.warn(`Windows timezone restored to "${process.env.STORED_TZ}"`);
} else {
process.env.TZ = process.env.STORED_TZ;
}
};
Run Code Online (Sandbox Code Playgroud)
这是我们使用它的示例。
// test file
import * as dtt from './dateTimeTools';
import typeOf from './funcs/typeOf';
import { restoreTimezone, setTimezone } from './setTimezone';
const inputs = {
dateOnly: '2020-01-01',
dateTimeWithoutTimezone: '2020-01-01T00:00:00.000000',
midnightNewYorkTimezone: '2020-01-01T00:00:00.000000-05:00',
};
describe('dateTimeTools > isoToDate', () => {
setTimezone('Pacific Standard Time'); //
assert({
given: 'any valid ISO string.',
should: 'return a date object.',
actual: typeOf(dtt.isoToDate(inputs.dateOnly)),
expected: 'date',
});
assert({
given: 'a date-only ISO string.',
should: 'return the given date at midnight in the local timezone.',
actual: dtt.isoToDate(inputs.dateOnly).toString(),
expected: 'Wed Jan 01 2020 00:00:00 GMT-0800 (Pacific Standard Time)',
});
restoreTimezone(); //
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4554 次 |
| 最近记录: |