bun:test lets you change what time it is in your tests.
This works with any of the following:
Date.nownew Date()new Intl.DateTimeFormat().format()
Timers are not impacted yet, but may be in a future release of Bun.
setSystemTime
To change the system time, usesetSystemTime:
test.ts
useFakeTimers and useRealTimers, you can use useFakeTimers and useRealTimers:
test.ts
Reset the system time
To reset the system time, pass no arguments tosetSystemTime:
test.ts
Get mocked time with jest.now()
When you’re using mocked time (withsetSystemTime or useFakeTimers), you can use jest.now() to get the current mocked timestamp:
test.ts
Set the time zone
By default, the time zone for allbun test runs is set to UTC (Etc/UTC) unless overridden. To change the time zone, either pass the $TZ environment variable to bun test:
terminal
process.env.TZ at runtime:
test.ts
Unlike in Jest, you can set the timezone multiple times at runtime and it will work.