test: implement timezone mock helper for Vitest test suite#5947
Open
MasterJi27 wants to merge 2 commits into
Open
test: implement timezone mock helper for Vitest test suite#5947MasterJi27 wants to merge 2 commits into
MasterJi27 wants to merge 2 commits into
Conversation
Aamod-Dev
approved these changes
Jun 18, 2026
Aamod-Dev
left a comment
Collaborator
There was a problem hiding this comment.
Difficulty: intermediate – New timezone-mock.ts helper (275 lines) with mockTimezone/restoreTimezone/withTimezone functions, 256-line test suite covering edge cases.
Quality: clean – Comprehensive testing utility with Intl.DateTimeFormat mocking, offset calculations, common timezone constants.
Type: testing – Reusable test infrastructure for timezone testing.
Excellent test utility!
Closes JhaSourav07#5274 - Create test-utils/timezone-mock.ts with mockTimezone(), restoreTimezone(), withTimezone(), getTimezoneOffsetMinutes(), and TEST_TIMEZONES exports - Mock Intl.DateTimeFormat to default to target timezone when no explicit timeZone option is provided - Set process.env.TZ for code paths that rely on environment variable - Include offset lookup table for 50+ IANA timezone identifiers including fractional offsets (Asia/Kathmandu UTC+5:45, Pacific/Chatham UTC+12:45) - Fallback to real Intl.DateTimeFormat for unrecognised timezone strings - Add 26 comprehensive tests covering all exported functions and integration with Intl.DateTimeFormat.formatToParts under fake timers
a131501 to
540ce49
Compare
Contributor
📦 Next.js Bundle Size Report (Gzipped Sizes)✨ No significant bundle size changes detected. 📊 Summary of Totals
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #5274
Problem
Timezone-sensitive tests fail on CI servers because the system timezone defaults to UTC, causing
Intl.DateTimeFormatto produce different results than on developer machines.Solution
Created a reusable timezone mock helper at
test-utils/timezone-mock.tsthat:mockTimezone(tz)— Setsprocess.env.TZand overridesIntl.DateTimeFormatto default to the target timezone.restoreTimezone()— Restores bothprocess.env.TZandIntl.DateTimeFormatto their original values.withTimezone(tz, fn)— Convenience wrapper that auto-restores even if the callback throws.getTimezoneOffsetMinutes(tz)— Returns the UTC offset in minutes for a given IANA timezone string.TEST_TIMEZONES— A readonly array of 13 common timezone identifiers for parameterised tests.Implementation Details
Intl.DateTimeFormatfor timezone strings not in the lookup tableRangeErrorfor invalid timezone identifiers (matching native behavior)Verification
npx vitest run test-utils/timezone-mock.test.ts✅Files Changed
test-utils/timezone-mock.ts— New timezone mock helper moduletest-utils/timezone-mock.test.ts— 26 comprehensive tests