Runtime
Bundler
Package Manager
Test Runner
Guides
Reference
Blog
Install Bun
Guides Test Runner

Set the system time in Bun's test runner

Set the system time in tests with the setSystemTime function from bun:test.

import { test, expect, setSystemTime } from "bun:test";

test("party like it's 1999", () => {
  const date = new Date("1999-01-01T00:00:00.000Z");
  setSystemTime(date); // it's now January 1, 1999

  const now = new Date();
  expect(now.getFullYear()).toBe(1999);
  expect(now.getMonth()).toBe(0);
  expect(now.getDate()).toBe(1);
});

Call setSystemTime in a lifecycle hook like beforeAll to give your tests a deterministic "fake clock".

import { test, expect, beforeAll, setSystemTime } from "bun:test";

beforeAll(() => {
  const date = new Date("1999-01-01T00:00:00.000Z");
  setSystemTime(date); // it's now January 1, 1999
});

// tests...

To reset the system clock to the actual time, call setSystemTime with no arguments.

import { test, expect, beforeAll, setSystemTime } from "bun:test";

setSystemTime(); // reset to actual time

See Dates and times.

On this page

No Headings