"useFakeTimers is not supported in this environment" when testing timezone-dependent Intl formatting
0 reputation · 13 Jan 2024, 09:18 UTC
0 reputation · 13 Jan 2024, 09:18 UTC
I need deterministic tests for date formatting that uses Intl.DateTimeFormat with an explicit timeZone option (for example America/New_York). The plan is to freeze the clock with jest.useFakeTimers() and jest.setSystemTime(new Date('2026-10-12T00:00:00Z')) so assertions on formatted output are stable.
Two issues are unclear. First, jest.useFakeTimers() throws "useFakeTimers is not supported in this environment" in environments without timer support, and it is not obvious which Jest test environments (node vs jsdom) and Jest versions (29+ assumed) reliably allow it. Second, even when fake timers work, setSystemTime() appears not to affect how Intl.DateTimeFormat resolves time zones — the formatted output still follows the host's default zone rather than anything controlled by the mock. The documented guidance seems to be setting the TZ environment variable or using a polyfill, but no settled behavior exists across releases.
1. In which Jest environments and versions is the "useFakeTimers is not supported in this environment" error expected, and what triggers it?
2. Is setSystemTime() intended to influence Intl.DateTimeFormat time-zone calculations, or is setting TZ before the test process starts the only supported approach?
3. Is there a documented way to verify that the mocked system time is actually being applied to Intl formatting inside a test?
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.