From 98c18173965d97688e4ec90052cbbc8298880fca Mon Sep 17 00:00:00 2001 From: Tom Caiger Date: Wed, 12 Jul 2023 14:54:36 +1200 Subject: [PATCH] use jest config to set test timezone --- packages/utils/jest-setup.js | 8 ++++++++ packages/utils/jest.config.js | 1 + .../src/__tests__/period/periodGranularities.test.js | 6 ++---- packages/web-frontend/config/jest/jest-setup.js | 8 ++++++++ packages/web-frontend/jest.config.js | 1 + packages/web-frontend/package.json | 1 - packages/web-frontend/src/tests/testutil.js | 3 --- packages/web-frontend/src/utils/formatDateForApi.js | 5 ++--- yarn.lock | 1 - 9 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 packages/utils/jest-setup.js create mode 100644 packages/web-frontend/config/jest/jest-setup.js diff --git a/packages/utils/jest-setup.js b/packages/utils/jest-setup.js new file mode 100644 index 0000000000..37603bc2fa --- /dev/null +++ b/packages/utils/jest-setup.js @@ -0,0 +1,8 @@ +/* + * Tupaia + * Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd + */ + +module.exports = async () => { + process.env.TZ = 'Australia/Melbourne'; +}; diff --git a/packages/utils/jest.config.js b/packages/utils/jest.config.js index 20b306c338..7a00334117 100644 --- a/packages/utils/jest.config.js +++ b/packages/utils/jest.config.js @@ -3,4 +3,5 @@ const baseConfig = require('../../jest.config-js.json'); module.exports = async () => ({ ...baseConfig, rootDir: '.', + globalSetup: '/jest-setup.js', }); diff --git a/packages/utils/src/__tests__/period/periodGranularities.test.js b/packages/utils/src/__tests__/period/periodGranularities.test.js index 2ec828adc0..b8e2cff466 100644 --- a/packages/utils/src/__tests__/period/periodGranularities.test.js +++ b/packages/utils/src/__tests__/period/periodGranularities.test.js @@ -3,7 +3,7 @@ * Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd */ -import moment from 'moment-timezone'; +import moment from 'moment'; import sinon from 'sinon'; import { roundStartEndDates, getDefaultDates, getLimits } from '../../period/periodGranularities'; @@ -11,15 +11,13 @@ const DEFAULT_NOW_TIMESTAMP = 1549360800000; // 2019-02-05T10:00:00.000Z const mockNow = (whenIsNow = DEFAULT_NOW_TIMESTAMP) => { sinon.useFakeTimers(whenIsNow); - moment.tz.setDefault('Australia/Melbourne'); }; const resetMocks = () => { sinon.restore(); - moment.tz.setDefault(); }; -describe.skip('chartGranularities', () => { +describe('chartGranularities', () => { beforeEach(() => { mockNow(1549360800 * 1000); // (2019-02-05 10:00 UTC) }); diff --git a/packages/web-frontend/config/jest/jest-setup.js b/packages/web-frontend/config/jest/jest-setup.js new file mode 100644 index 0000000000..37603bc2fa --- /dev/null +++ b/packages/web-frontend/config/jest/jest-setup.js @@ -0,0 +1,8 @@ +/* + * Tupaia + * Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd + */ + +module.exports = async () => { + process.env.TZ = 'Australia/Melbourne'; +}; diff --git a/packages/web-frontend/jest.config.js b/packages/web-frontend/jest.config.js index a08a2bda3a..1d479d6c65 100644 --- a/packages/web-frontend/jest.config.js +++ b/packages/web-frontend/jest.config.js @@ -3,6 +3,7 @@ const baseConfig = require('../../jest.config-js.json'); module.exports = { ...baseConfig, rootDir: '.', + globalSetup: '/config/jest/jest-setup.js', setupFiles: ['/config/polyfills.js'], testMatch: ['/src/**/__tests__/**/*.js?(x)', '/src/**/?(*.)(spec|test).js?(x)'], testURL: 'http://localhost', diff --git a/packages/web-frontend/package.json b/packages/web-frontend/package.json index 1b8760f83f..34ee6d4ad8 100644 --- a/packages/web-frontend/package.json +++ b/packages/web-frontend/package.json @@ -57,7 +57,6 @@ "material-ui": "^0.18.3", "material-ui-datetimepicker": "^1.0.7", "moment": "^2.21.0", - "moment-timezone": "^0.5.28", "numeral": "^2.0.6", "polished": "^3.0.0", "prop-types": "^15.6.2", diff --git a/packages/web-frontend/src/tests/testutil.js b/packages/web-frontend/src/tests/testutil.js index e3d2a69aee..aab56a85ae 100644 --- a/packages/web-frontend/src/tests/testutil.js +++ b/packages/web-frontend/src/tests/testutil.js @@ -1,14 +1,11 @@ import sinon from 'sinon'; -import moment from 'moment-timezone'; const DEFAULT_NOW_TIMESTAMP = 1549360800000; // 2019-02-05T10:00:00.000Z export const mockNow = (whenIsNow = DEFAULT_NOW_TIMESTAMP) => { sinon.useFakeTimers(whenIsNow); - moment.tz.setDefault('Australia/Melbourne'); }; export const resetMocks = () => { sinon.restore(); - moment.tz.setDefault(); }; diff --git a/packages/web-frontend/src/utils/formatDateForApi.js b/packages/web-frontend/src/utils/formatDateForApi.js index 91f4a47106..da03677b19 100644 --- a/packages/web-frontend/src/utils/formatDateForApi.js +++ b/packages/web-frontend/src/utils/formatDateForApi.js @@ -5,13 +5,12 @@ * found in the LICENSE file in the root directory of this source tree. */ -import moment from 'moment-timezone'; +import moment from 'moment'; const DATE_FORMAT = 'YYYY-MM-DD'; -export const formatDateForApi = (date, timezone) => { +export const formatDateForApi = date => { if (!date) return undefined; const dateAsMoment = moment(date); - if (timezone) dateAsMoment.tz(timezone); return dateAsMoment.format(DATE_FORMAT); }; diff --git a/yarn.lock b/yarn.lock index 844f1a6f0a..a7fab8138f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10859,7 +10859,6 @@ __metadata: material-ui-datetimepicker: ^1.0.7 mockdate: ^3.0.5 moment: ^2.21.0 - moment-timezone: ^0.5.28 npm-run-all: ^4.1.5 numeral: ^2.0.6 object-assign: 4.1.1