Fixing the Current Time in Bun Tests with setSystemTime
In bun test, passing a date to setSystemTime from bun:test makes Date.now() and new Date() in the test return that date. Because you can fix the current time, you can write tests for code whose result depends on the current time, such as whether a campaign deadline has passed or what today's date is in Japan, so the test gives the same result no matter when you run it.
This is useful when you want to check the moments just before and after a deadline, or the boundary where the date changes, without waiting for that time. In my test environment, I ran tests with a fixed time and checked whether the clock advances, how far a missed reset leaks, and whether the time zone changes the results.
The test environment was Ubuntu 24.04 (x86_64) with Bun 1.4.4. I wrote the tests in TypeScript and used only bun:test (no packages installed). I did not try Windows or macOS, other Bun versions, jest.useFakeTimers (the Jest-style API), or jest.advanceTimersByTime for advancing timers.
Checking Before and After a Deadline by Fixing the Time
I tested isOpen, which returns whether entries are still accepted up to 2026-12-31 23:59:59 JST, and todayInTokyo, which returns today's date in Japan time. Both read the current time internally.
// src/campaign.ts
// Campaign deadline (2026-12-31 23:59:59 JST)
const DEADLINE = new Date("2026-12-31T23:59:59+09:00");
// Whether the campaign is open for entries right now
export function isOpen(): boolean {
return Date.now() <= DEADLINE.getTime();
}
// Format today's date in Japan time as "2026/12/31"
export function todayInTokyo(): string {
return new Intl.DateTimeFormat("ja-JP", {
timeZone: "Asia/Tokyo",
year: "numeric",
month: "2-digit",
day: "2-digit",
}).format(new Date());
}
In the tests, I pass the time I want to check to setSystemTime and then call the function. Calling setSystemTime with no arguments returns to the real time (the official documentation on restoring the time), so I restore it in afterEach after every test.
// tests/campaign.test.ts
import { test, expect, setSystemTime, afterEach } from "bun:test";
import { isOpen, todayInTokyo } from "../src/campaign";
afterEach(() => {
setSystemTime(); // Restore the real time
});
test("締め切りちょうどまでは申し込める", () => {
setSystemTime(new Date("2026-12-31T23:59:59+09:00"));
expect(isOpen()).toBe(true);
});
test("締め切りの1秒後は申し込めない", () => {
setSystemTime(new Date("2027-01-01T00:00:00+09:00"));
expect(isOpen()).toBe(false);
});
test("UTC の15時は、日本時間ではもう次の日", () => {
setSystemTime(new Date("2026-12-31T15:00:00Z"));
expect(todayInTokyo()).toBe("2027/01/01");
});
$ bun test ./tests/campaign.test.ts
bun test v1.4.4 (38afee6c8)
3 pass
0 fail
3 expect() calls
Ran 3 tests across 1 file. [16.00ms]
I ran these on 2026-11-26, which is before the deadline. Even so, the test that fixed the time to 1 second after the deadline made isOpen() return false. The format() call in Intl.DateTimeFormat also built its date from the fixed time, so 15:00 UTC became "2027/01/01", the next day in Japan time. The official documentation on dates and times states that setSystemTime affects Date.now(), new Date(), and Intl.DateTimeFormat().format().
The Clock Stays Stopped at the Fixed Time
After fixing the time with setSystemTime, I waited 100 milliseconds and then compared Date.now(). For comparison, I also measured the difference in performance.now(). The output below is only the lines from this test when I ran bun test ./tests/frozen.test.ts.
// tests/frozen.test.ts (first test)
test("決めた時刻のまま、時計は進まない", async () => {
setSystemTime(new Date("2026-12-31T23:59:00+09:00"));
const before = Date.now();
const p0 = performance.now();
await Bun.sleep(100);
console.log("Date.now の差:", Date.now() - before, "ms");
console.log("performance.now の差:", Math.round(performance.now() - p0), "ms");
console.log("new Date():", new Date().toISOString());
setSystemTime();
});
Date.now の差: 0 ms
performance.now の差: 100 ms
new Date(): 2026-12-31T14:59:00.000Z
Even though 100 milliseconds really passed, the difference in Date.now() was 0. The clock does not advance from the fixed time. Code that measures elapsed time with the difference in Date.now() will report 0 milliseconds inside a test where the time is fixed. In contrast, performance.now() advanced in real time.
To move the time forward, call setSystemTime again. The argument can be a Date, or a number of milliseconds (the type definition for setSystemTime).
// tests/frozen.test.ts (second test)
test("時刻を進めたいときは、もう一度呼ぶ", () => {
const start = new Date("2026-12-31T23:59:00+09:00");
setSystemTime(start);
setSystemTime(start.getTime() + 60_000); // A number of milliseconds also works
console.log("1分後:", new Date().toISOString());
expect(Date.now() - start.getTime()).toBe(60_000);
setSystemTime();
});
1分後: 2026-12-31T15:00:00.000Z
2 pass
0 fail
1 expect() calls
Ran 2 tests across 1 file. [104.00ms]
The final 2 pass line and the others are the results for the whole file, including the first test.
If You Forget to Reset, It Leaks Into Other Test Files
In the first test, I set the time to 2000-01-01 and did not reset it. In the next test in the same file, and in a test in a different file that runs afterward, I printed new Date().
// tests/a-leak.test.ts
import { test, setSystemTime } from "bun:test";
test("時刻を決めて、戻さない", () => {
setSystemTime(new Date("2000-01-01T00:00:00Z"));
console.log("a-leak 1つ目:", new Date().toISOString());
});
test("同じファイルの次のテスト", () => {
console.log("a-leak 2つ目:", new Date().toISOString());
});
// tests/b-next.test.ts
import { test } from "bun:test";
test("あとで動く、別のファイルのテスト", () => {
console.log("b-next:", new Date().toISOString());
});
$ bun test ./tests/a-leak.test.ts ./tests/b-next.test.ts
bun test v1.4.4 (38afee6c8)
tests/a-leak.test.ts:
a-leak 1つ目: 2000-01-01T00:00:00.000Z
a-leak 2つ目: 2000-01-01T00:00:00.000Z
tests/b-next.test.ts:
b-next: 2000-01-01T00:00:00.000Z
3 pass
0 fail
Ran 3 tests across 2 files. [4.00ms]
Even the test in b-next.test.ts, which never calls setSystemTime, still saw 2000-01-01. A test that does not know the time was fixed runs with a different date.
I replaced a-leak.test.ts with a-reset.test.ts, which resets the time after every test with afterEach, and ran it the same way. Apart from the test names and log labels, it is identical to a-leak.test.ts.
// tests/a-reset.test.ts (top part)
import { test, setSystemTime, afterEach } from "bun:test";
afterEach(() => {
setSystemTime(); // Restore the real time
});
$ bun test ./tests/a-reset.test.ts ./tests/b-next.test.ts
bun test v1.4.4 (38afee6c8)
tests/a-reset.test.ts:
a-reset 1つ目: 2000-01-01T00:00:00.000Z
a-reset 2つ目: 2026-11-26T13:30:53.988Z
tests/b-next.test.ts:
b-next: 2026-11-26T13:30:53.988Z
3 pass
0 fail
Ran 3 tests across 2 files. [3.00ms]
From the second test on, the time returned to the real time on the day I ran the test.
The time fixed with setSystemTime did not reset when the test finished. It remained in effect for other files that ran later in the same run, so call setSystemTime() in afterEach to restore it.
Even with a Fixed Time, the Time Zone Stays UTC
Code that reads the time in the environment's time zone, such as getHours(), needs extra care. I ran a test that fixed 9:00 AM in Japan time and checked getHours() without setting TZ.
// tests/tz.test.ts
import { test, expect, setSystemTime } from "bun:test";
test("日本時間の朝9時", () => {
setSystemTime(new Date("2026-12-01T09:00:00+09:00"));
console.log("TZ:", Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log("getHours():", new Date().getHours());
expect(new Date().getHours()).toBe(9);
setSystemTime();
});
$ bun test ./tests/tz.test.ts
bun test v1.4.4 (38afee6c8)
tests/tz.test.ts:
TZ: UTC
getHours(): 0
2 |
3 | test("日本時間の朝9時", () => {
4 | setSystemTime(new Date("2026-12-01T09:00:00+09:00"));
5 | console.log("TZ:", Intl.DateTimeFormat().resolvedOptions().timeZone);
6 | console.log("getHours():", new Date().getHours());
7 | expect(new Date().getHours()).toBe(9);
^
error: expect(received).toBe(expected)
Expected: 9
Received: 0
at <anonymous> (/path/to/app/tests/tz.test.ts:7:33)
(fail) 日本時間の朝9時 [7.82ms]
0 pass
1 fail
1 expect() calls
Ran 1 test across 1 file. [10.00ms]
The fixed time was 9:00 in Japan time, which is 0:00 UTC. The time zone inside the test was UTC, so getHours() returned 0. The official documentation section on setting the time zone says that bun test uses UTC (Etc/UTC) by default. I switched the OS time zone on the test machine to Japan time (Asia/Tokyo) and tried again. A script run with bun -e showed Asia/Tokyo inside, but bun test still printed TZ: UTC and getHours(): 0 and failed. Even when the OS time zone is Japan time, the inside of bun test is UTC.
To change the time zone, pass the environment variable TZ to bun test.
$ TZ=Asia/Tokyo bun test ./tests/tz.test.ts
bun test v1.4.4 (38afee6c8)
tests/tz.test.ts:
TZ: Asia/Tokyo
getHours(): 9
1 pass
0 fail
1 expect() calls
Ran 1 test across 1 file. [10.00ms]
The same official documentation also describes assigning to process.env.TZ inside the test. In a file that assigns process.env.TZ = "Asia/Tokyo" in beforeAll, getHours() returned 9 and the test passed, even without setting TZ on the command line.
// tests/tz-env.test.ts
import { test, expect, setSystemTime, beforeAll } from "bun:test";
beforeAll(() => {
process.env.TZ = "Asia/Tokyo";
});
test("日本時間の朝9時", () => {
setSystemTime(new Date("2026-12-01T09:00:00+09:00"));
expect(new Date().getHours()).toBe(9);
setSystemTime();
});
$ bun test ./tests/tz-env.test.ts
bun test v1.4.4 (38afee6c8)
1 pass
0 fail
1 expect() calls
Ran 1 test across 1 file. [8.00ms]
Code like todayInTokyo at the beginning, which specifies timeZone: "Asia/Tokyo" inside the function, returned the Japan-time date even though the test's time zone stayed UTC. The results depend on the test's time zone only for code that uses the environment's time zone, such as getHours() or getDate().





