This question is similar to How do I set a timezone in my Jest config? but all the answers there pertain to *nix.
How do you set the timezone for jest tests when running Windows 10?
This question is similar to How do I set a timezone in my Jest config? but all the answers there pertain to *nix.
How do you set the timezone for jest tests when running Windows 10?
Share Improve this question asked Apr 22, 2020 at 21:29 GollyJerGollyJer 26.9k17 gold badges124 silver badges186 bronze badges 1- It seems there is no way to do it without affectin system time in Windows. You could check set-tz code for details. Upd: please take a look at this solution as well. – Shlang Commented Apr 23, 2020 at 8:00
2 Answers
Reset to default 3OK! Thanks for the ments and answers. It got me pointed in a good direction.
set-tz
is cool but the cleanup()
function wasn't firing when the tests pleted.
That, bined with tzutil
changing the Windows system time, forced me to think about minimizing the impact of the timezone change and having a way to force it back instead of relying on an exit-listener.
So, here's where it landed.
// 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;
}
};
Here's an example of where we use it.
// 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'); //
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745041372a4607826.html
评论列表(0条)