Skip to main content
bun:test lets you change what time it is in your tests. This works with any of the following:
  • Date.now
  • new 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, use setSystemTime:
test.ts
To support existing tests that use Jest’s useFakeTimers and useRealTimers, you can use useFakeTimers and useRealTimers:
test.ts
Timers — Note that we have not implemented builtin support for mocking timers yet, but this is on the roadmap.

Reset the system time

To reset the system time, pass no arguments to setSystemTime:
test.ts

Get mocked time with jest.now()

When you’re using mocked time (with setSystemTime or useFakeTimers), you can use jest.now() to get the current mocked timestamp:
test.ts
This is useful when you need to access the mocked time directly without creating a new Date object.

Set the time zone

By default, the time zone for all bun 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
Or set process.env.TZ at runtime:
test.ts
Unlike in Jest, you can set the timezone multiple times at runtime and it will work.