From db3d0486e750e9aa48067d970d332d86ecd92f56 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Thu, 10 Jun 2021 12:59:33 -0700 Subject: [PATCH] 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 --- desktop/package.json | 10 +++++----- desktop/scripts/jest-setup.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/desktop/package.json b/desktop/package.json index 357621859..269a4cff9 100644 --- a/desktop/package.json +++ b/desktop/package.json @@ -250,13 +250,13 @@ "start": "yarn dev-server --inspect=9229", "start:break": "yarn dev-server --inspect-brk=9229", "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-electron": "yarn build:tsc && 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:debug": "yarn build:tsc && node --inspect node_modules/.bin/jest --runInBand", + "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 && cross-env TZ=Pacific/Pohnpei USE_ELECTRON_STUBS=1 jest --testMatch=\"**/**.device\\.(js|jsx|ts|tsx)\" --detectOpenHandles", + "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", - "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", "workspaces": { diff --git a/desktop/scripts/jest-setup.js b/desktop/scripts/jest-setup.js index 5cca3996c..7d8ff848f 100644 --- a/desktop/scripts/jest-setup.js +++ b/desktop/scripts/jest-setup.js @@ -11,6 +11,22 @@ // eslint-disable-next-line 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('../app/src/fb-stubs/Logger').init(undefined, {isTest: true});