Make sure all tests run in same TimeZone

Summary:
Not being able to have timestamps in Jest snapshots, as they would differ across machines required jumping through a lot of hoops.

Run this into once again more by a test suite that didn't trigger on CI, but failed for me locally.

With this fix everyone can happily commit time based snapshots and they will always be assumed to be running in UTC :)

Reviewed By: nikoant

Differential Revision: D29025759

fbshipit-source-id: 426d1b065afdd4a2ed75b47203b13fff0ece1272
This commit is contained in:
Michel Weststrate
2021-06-10 12:59:33 -07:00
committed by Facebook GitHub Bot
parent 7e4df00138
commit db3d0486e7
2 changed files with 21 additions and 5 deletions

View File

@@ -250,13 +250,13 @@
"start": "yarn dev-server --inspect=9229", "start": "yarn dev-server --inspect=9229",
"start:break": "yarn dev-server --inspect-brk=9229", "start:break": "yarn dev-server --inspect-brk=9229",
"start:no-embedded-plugins": "yarn start --no-embedded-plugins", "start:no-embedded-plugins": "yarn start --no-embedded-plugins",
"test": "jest", "test": "cross-env TZ=Pacific/Pohnpei jest",
"test-e2e": "cd e2e && yarn test", "test-e2e": "cd e2e && yarn test",
"test-electron": "yarn build:tsc && jest --testMatch=\"**/**.electron\\.(js|jsx|ts|tsx)\" --testEnvironment=@jest-runner/electron/environment --runner=@jest-runner/electron", "test-electron": "yarn build:tsc && cross-env TZ=Pacific/Pohnpei jest --testMatch=\"**/**.electron\\.(js|jsx|ts|tsx)\" --testEnvironment=@jest-runner/electron/environment --runner=@jest-runner/electron",
"test-with-device": "yarn build:tsc && USE_ELECTRON_STUBS=1 jest --testMatch=\"**/**.device\\.(js|jsx|ts|tsx)\" --detectOpenHandles", "test-with-device": "yarn build:tsc && cross-env TZ=Pacific/Pohnpei USE_ELECTRON_STUBS=1 jest --testMatch=\"**/**.device\\.(js|jsx|ts|tsx)\" --detectOpenHandles",
"test:debug": "yarn build:tsc && node --inspect node_modules/.bin/jest --runInBand", "test:debug": "yarn build:tsc && cross-env TZ=Pacific/Pohnpei node --inspect node_modules/.bin/jest --runInBand",
"tsc-plugins": "./ts-node scripts/tsc-plugins.ts", "tsc-plugins": "./ts-node scripts/tsc-plugins.ts",
"watch": "node --expose-gc --stack-trace-limit=40 ./node_modules/.bin/jest --watch" "watch": "cross-env TZ=Pacific/Pohnpei node --expose-gc --stack-trace-limit=40 ./node_modules/.bin/jest --watch"
}, },
"version": "0.93.0", "version": "0.93.0",
"workspaces": { "workspaces": {

View File

@@ -11,6 +11,22 @@
// eslint-disable-next-line // eslint-disable-next-line
global.fetch = require('jest-fetch-mock'); global.fetch = require('jest-fetch-mock');
// make sure test run everywhere in the same timezone!
// +11, somewhere in the middle of nowhere, so this deviates for everyone and will fail early :)
const timezone = 'Pacific/Pohnpei';
if (process.env.TZ !== timezone) {
throw new Error(
`Test started in the wrong timezone, got ${process.env.TZ}, but expected ${timezone}. Please use the package.json commands to start Jest, or prefix the command with TZ=${timezone}`,
);
}
// Make sure we have identical formatting of Dates everywhere
const toLocaleString = Date.prototype.toLocaleString;
// eslint-disable-next-line no-extend-native
Date.prototype.toLocaleString = function (_locale, ...args) {
return toLocaleString.call(this, 'en-US', ...args);
};
require('immer').enableMapSet(); require('immer').enableMapSet();
require('../app/src/fb-stubs/Logger').init(undefined, {isTest: true}); require('../app/src/fb-stubs/Logger').init(undefined, {isTest: true});