test: fix timezone issues in EventDisplay tests

- Set explicit UTC timezone globally in jest.config.ts and setupTests.ts
- Mock Intl.DateTimeFormat to use UTC by default while preserving prototype
- Replace regex patterns with exact values for declarative assertions
- Remove complex date formatting computations in tests
- Use fixed dates (2025-01-15T10:00:00.000Z) for consistent test results
- Fix EventDisplay.test.tsx to use exact time values instead of regex
This commit is contained in:
lenhanphung
2025-10-10 15:04:03 +07:00
committed by Benoit TELLIER
parent 59e7992cfe
commit 192ab17789
3 changed files with 59 additions and 46 deletions
+27
View File
@@ -4,6 +4,33 @@ import { TextEncoder } from "util";
global.TextEncoder = TextEncoder;
// Set timezone to UTC for consistent test results across all environments
process.env.TZ = "UTC";
// Mock Intl.DateTimeFormat to use UTC timezone by default
const OriginalDateTimeFormat = Intl.DateTimeFormat;
// Create a constructor function that preserves prototype
const MockedDateTimeFormat: any = function (
this: any,
locales?: string | string[],
options?: Intl.DateTimeFormatOptions
) {
// Only set timeZone to UTC if not explicitly specified
const modifiedOptions = options
? { timeZone: "UTC", ...options } // timeZone comes first so options can override it
: { timeZone: "UTC" };
return new OriginalDateTimeFormat(locales, modifiedOptions);
};
// Preserve the prototype so jest.spyOn works on methods
MockedDateTimeFormat.prototype = OriginalDateTimeFormat.prototype;
// Preserve static methods
Object.setPrototypeOf(MockedDateTimeFormat, OriginalDateTimeFormat);
global.Intl.DateTimeFormat = MockedDateTimeFormat;
jest.mock("openid-client", () => ({
discovery: jest.fn(),
randomPKCECodeVerifier: jest.fn(),