@ende/testing/time

ende add jsr:@ende/testing

Version 2026.10.15 on JSR.

Utilities for mocking time while testing.

import {
  assertSpyCalls,
  spy,
} from "@ende/testing/mock";
import { FakeTime } from "@ende/testing/time";

function secondInterval(cb: () => void) {
  return setInterval(cb, 1000);
}

Ende.test("secondInterval calls callback every second and stops after being cleared", () => {
  using time = new FakeTime();

  const cb = spy();
  const intervalId = secondInterval(cb);
  assertSpyCalls(cb, 0);
  time.tick(500);
  assertSpyCalls(cb, 0);
  time.tick(500);
  assertSpyCalls(cb, 1);
  time.tick(3500);
  assertSpyCalls(cb, 4);

  clearInterval(intervalId);
  time.tick(1000);
  assertSpyCalls(cb, 4);
});

Exports

  • DelayOptions — Options for delay.
  • FakeTime — Overrides the real Date object and timer functions with fake ones that can be controlled through the fake time instance.
  • FakeTimeOptions — The option for FakeTime
  • TimeError — Represents an error when trying to execute an invalid operation on fake time, given the state fake time is in.