Skip to main content
On this page

@std/testing

Overview Jump to heading

This package provides utilities for 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);
});

Add to your project Jump to heading

deno add jsr:@std/testing

See all symbols in @std/testing on

Testing Jump to heading

Testing is the practice of verifying that your code behaves as expected. It helps catch bugs early, ensures code quality, and provides confidence when making changes.

Check out the Deno testing examples for practical usage.

Why use @std/testing Jump to heading

Use these utilities alongside Deno’s built-in Deno.test to write clearer specs, mock dependencies, fake timers, and create snapshots.

Examples Jump to heading

import { afterEach, beforeEach, describe, it } from "@std/testing/bdd";
import { assertSpyCalls, spy } from "@std/testing/mock";
import { FakeTime } from "@std/testing/time";

describe("interval", () => {
  let time: FakeTime;
  beforeEach(() => (time = new FakeTime()));
  afterEach(() => time.restore());

  it("ticks", () => {
    const cb = spy();
    const id = setInterval(cb, 1000);
    time.tick(3000);
    assertSpyCalls(cb, 3);
    clearInterval(id);
  });
});

Snapshot testing Jump to heading

import { assertSnapshot } from "@std/testing/snapshot";

Deno.test("user json snapshot", async (t) => {
  const user = { id: 1, name: "Ada", tags: ["admin", "ops"] };
  await assertSnapshot(t, user);
});

Stub a global method Jump to heading

import { stub } from "@std/testing/mock";
import { assertEquals } from "@std/assert";

Deno.test("stub Math.random", () => {
  const s = stub(Math, "random", () => 0.5);
  try {
    assertEquals(Math.random(), 0.5);
  } finally {
    s.restore();
  }
});

Program a sequence with returnsNext Jump to heading

import { returnsNext } from "@std/testing/mock";
import { assertEquals, assertThrows } from "@std/assert";

Deno.test("returnsNext sequences values and errors", () => {
  const next = returnsNext([1, 2, new Error("boom"), 3]);
  assertEquals(next(), 1);
  assertEquals(next(), 2);
  assertThrows(() => next(), Error, "boom");
  assertEquals(next(), 3);
});

Spy and assert call arguments Jump to heading

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

Deno.test("spy captures calls and args", () => {
  const sum = spy((a: number, b: number) => a + b);
  sum(3, 4);
  sum(5, 6);
  assertSpyCalls(sum, 2);
  assertSpyCallArgs(sum, 0, [3, 4]);
});

Tips Jump to heading

  • Combine with @std/assert or @std/expect for assertions.
  • Use FakeTime to deterministically test timers and Date.now().
  • Restore stubs/spies in afterEach or finally blocks to avoid leaking state.
  • Commit snapshot files to version control to catch unintentional changes.

Did you find what you needed?

Privacy policy