Skip to content

test: implement timezone mock helper for Vitest test suite#5947

Open
MasterJi27 wants to merge 2 commits into
JhaSourav07:mainfrom
MasterJi27:test/timezone-mock-helper-5274
Open

test: implement timezone mock helper for Vitest test suite#5947
MasterJi27 wants to merge 2 commits into
JhaSourav07:mainfrom
MasterJi27:test/timezone-mock-helper-5274

Conversation

@MasterJi27

Copy link
Copy Markdown
Contributor

Summary

Closes #5274

Problem

Timezone-sensitive tests fail on CI servers because the system timezone defaults to UTC, causing Intl.DateTimeFormat to produce different results than on developer machines.

Solution

Created a reusable timezone mock helper at test-utils/timezone-mock.ts that:

  1. mockTimezone(tz) — Sets process.env.TZ and overrides Intl.DateTimeFormat to default to the target timezone.
  2. restoreTimezone() — Restores both process.env.TZ and Intl.DateTimeFormat to their original values.
  3. withTimezone(tz, fn) — Convenience wrapper that auto-restores even if the callback throws.
  4. getTimezoneOffsetMinutes(tz) — Returns the UTC offset in minutes for a given IANA timezone string.
  5. TEST_TIMEZONES — A readonly array of 13 common timezone identifiers for parameterised tests.

Implementation Details

  • Includes a lookup table of 50+ IANA timezone identifiers with their UTC offsets, including fractional offsets (Asia/Kathmandu UTC+5:45, Pacific/Chatham UTC+12:45)
  • Falls back to the real Intl.DateTimeFormat for timezone strings not in the lookup table
  • Throws RangeError for invalid timezone identifiers (matching native behavior)

Verification

  • 26 tests all passing locally
  • npx vitest run test-utils/timezone-mock.test.ts

Files Changed

  • test-utils/timezone-mock.ts — New timezone mock helper module
  • test-utils/timezone-mock.test.ts — 26 comprehensive tests

@github-actions github-actions Bot added the status:blocked This PR is blocked due to a failing CI check. label Jun 17, 2026
@Aamod-Dev Aamod-Dev added level:intermediate Moderate complexity tasks quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. type:testing Adding, updating, or fixing tests mentor:Aamod007 labels Jun 18, 2026

@Aamod-Dev Aamod-Dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@MasterJi27 MasterJi27 force-pushed the test/timezone-mock-helper-5274 branch from a131501 to 540ce49 Compare June 18, 2026 19:08
@github-actions github-actions Bot removed the status:blocked This PR is blocked due to a failing CI check. label Jun 18, 2026
@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

📦 Next.js Bundle Size Report (Gzipped Sizes)

✨ No significant bundle size changes detected.

📊 Summary of Totals

Category PR Size Base Size Difference
Total JS 3649.55 KB 3649.55 KB 0 B
Total CSS 287.99 KB 287.99 KB 0 B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:intermediate Moderate complexity tasks mentor:Aamod007 quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. type:testing Adding, updating, or fixing tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: Implement timezone mock helper for Vitest test suite

2 participants