new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Test runner · all subjects

dates-times

12 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

setSystemTime function for changing test time

The setSystemTime function from bun:test allows you to change what time it is in your tests. It affects Date.now(), new Date(), and new Intl.DateTimeFormat().format(). Pass a Date object to set the time to a specific moment.

Reset system time by calling setSystemTime with no arguments

To reset the system time to the actual current time during a test, call setSystemTime() with no arguments.

jest.useFakeTimers and jest.useRealTimers work in bun:test

bun:test supports Jest's useFakeTimers() and useRealTimers() functions for backward compatibility, so existing tests using these continue to work.

Date constructor does not change when using useFakeTimers in bun:test

Unlike Jest, when you call useFakeTimers in bun:test, the Date constructor itself does not change. The Date constructor reference remains the same (Date === Date before and after useFakeTimers), and Date.now remains the same reference. This avoids bugs that can occur in Jest when Date constructor identity changes.

jest.now() returns the current mocked timestamp

When time is mocked using setSystemTime or useFakeTimers, jest.now() returns the current mocked timestamp as a number. This allows you to read the mocked time without creating a new Date object.

Default timezone for bun test is UTC

By default, bun test runs in the UTC timezone (Etc/UTC).

Set timezone with TZ environment variable or process.env.TZ

To change the timezone in tests, either pass the TZ environment variable to bun test (e.g., TZ=America/Los_Angeles bun test) or set process.env.TZ at runtime in your test code.

bun:test allows changing timezone multiple times at runtime

Unlike Jest, bun:test allows you to change the timezone multiple times at runtime within the same test session by setting process.env.TZ, and the changes will take effect.

Example: setSystemTime with date assertion

import { setSystemTime, beforeAll, test, expect } from "bun:test"; beforeAll(() => { setSystemTime(new Date("2020-01-01T00:00:00.000Z")); }); test("it is 2020", () => { expect(new Date().getFullYear()).toBe(2020); });

Example: jest.useFakeTimers with jest.now()

import { test, expect, jest } from "bun:test"; test("get the current mocked time", () => { jest.useFakeTimers(); jest.setSystemTime(new Date("2020-01-01T00:00:00.000Z")); expect(Date.now()).toBe(1577836800000); // Jan 1, 2020 timestamp expect(jest.now()).toBe(1577836800000); // Same value jest.useRealTimers(); });

Example: changing timezone at runtime

import { test, expect } from "bun:test"; test("Welcome to California!", () => { process.env.TZ = "America/Los_Angeles"; expect(new Date().getTimezoneOffset()).toBe(420); expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe("America/Los_Angeles"); }); test("Welcome to New York!", () => { process.env.TZ = "America/New_York"; expect(new Date().getTimezoneOffset()).toBe(240); expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe("America/New_York"); });

TZ defaults to UTC in bun test

bun test uses UTC (Etc/UTC) as the time zone unless the TZ environment variable overrides it. This keeps date and time behavior consistent across machines.

Give your agent this brain