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:
committed by
Benoit TELLIER
parent
59e7992cfe
commit
192ab17789
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user