skills/bun-guides-test-mock-clock/SKILL.md
Set the system time in Bun's test runner
npx skillsauth add jarle/bun-skills Bun Set the system time in Bun's test runnerInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Bun's test runner supports setting the system time programmatically with the setSystemTime function.
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);
});
The setSystemTime function is commonly used on conjunction with Lifecycle Hooks to configure a testing environment with 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 Docs > Test Runner > Date and time for complete documentation on mocking with the Bun test runner.
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling