You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

35 lines
925 B

const NodeEnvironment = require('jest-environment-node').TestEnvironment;
// Jest gives each test file a copy of `process`, so a test cannot change the
// time zone at run time. This environment sets `TZ` on the real process of the
// worker, which resets the internal date cache of Node.
class TimeZoneEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
this.previousTimeZone = process.env.TZ;
this.timeZone = config.projectConfig.testEnvironmentOptions?.timeZone;
}
async setup() {
if (this.timeZone) {
process.env.TZ = this.timeZone;
}
await super.setup();
}
async teardown() {
try {
await super.teardown();
} finally {
if (this.previousTimeZone === undefined) {
delete process.env.TZ;
} else {
process.env.TZ = this.previousTimeZone;
}
}
}
}
module.exports = TimeZoneEnvironment;