Skip to main content
Home

@std/testing@1.0.16
Built and signed on GitHub Actions

Works with
This package works with Deno
This package works with Deno
JSR Score94%
Downloads11,159/wk
Published3 months ago (1.0.16)

Tools for testing Deno code like snapshot testing, bdd testing, and time mocking

Utilities for mocking time while testing.

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

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

Deno.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);
});

Classes

c
FakeTime

Overrides the real Date object and timer functions with fake ones that can be controlled through the fake time instance.

c
TimeError

Represents an error when trying to execute an invalid operation on fake time, given the state fake time is in.

Interfaces

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/testing

Import symbol

import * as mod from "@std/testing/time";
or

Import directly with a jsr specifier

import * as mod from "jsr:@std/testing/time";