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()

setSystemTime

To change the system time, use setSystemTime:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35btest.ts
Jest’s useFakeTimers and useRealTimers are also supported, so existing tests that use them keep working:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35btest.ts

Reset the system time

To reset the system time, pass no arguments to setSystemTime:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35btest.ts

Get mocked time with jest.now()

When the time is mocked (with setSystemTime or useFakeTimers), jest.now() returns the current mocked timestamp:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35btest.ts
Use it to read the mocked time without creating a new Date object.

Set the time zone

By default, bun test runs in UTC (Etc/UTC). To change the time zone, either pass the TZ environment variable to bun test:
terminal
Or set process.env.TZ at runtime:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35btest.ts
Unlike in Jest, you can change the time zone multiple times at runtime.