Skip to content

Testing in Deno with Deno.test and @std/assert

Deno has a test runner built into the CLI. You register tests with Deno.test, run them with deno test, and get type-checking, filtering and coverage without installing anything. The runner is part of the runtime; assertion helpers come from the standard library package @std/assert.

Put the code under test in math.ts:

export function add(a: number, b: number): number {
return a + b;
}
export function divide(a: number, b: number): number {
if (b === 0) throw new RangeError("Division by zero");
return a / b;
}

And the tests in math_test.ts:

import { assertEquals, assertThrows } from "jsr:@std/assert@1";
import { add, divide } from "./math.ts";
Deno.test("add sums two numbers", () => {
assertEquals(add(2, 3), 5);
});
Deno.test("divide throws on zero", () => {
assertThrows(() => divide(1, 0), RangeError, "Division by zero");
});

In a project with a deno.json, run deno add jsr:@std/assert once and import from "@std/assert" instead of the full jsr: specifier.

assertEquals compares deeply, so it works for objects and arrays as well as primitives. Other common ones are assert, assertStrictEquals, assertRejects (for promises), assertMatch and assertObjectMatch. If you prefer Jest-style syntax, @std/expect provides expect(x).toBe(y).

Terminal window
deno test

With no arguments, deno test finds files in the current directory tree whose names end in _test or .test, or are named test, with a .ts, .tsx, .mts, .js, .mjs or .jsx extension. You can also pass files or directories explicitly. Test files are type-checked before they run.

running 2 tests from ./math_test.ts
add sums two numbers ... ok (542µs)
divide throws on zero ... ok (735µs)
ok | 2 passed | 0 failed (77ms)

If the test function returns a promise, the runner awaits it. Tests get the same sandbox as deno run, so a test that makes a network request needs --allow-net:

import { assert, assertEquals } from "jsr:@std/assert@1";
Deno.test("fetches a post", async () => {
const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");
assert(response.ok);
const post = await response.json();
assertEquals(post.id, 1);
});
Terminal window
deno test --allow-net=jsonplaceholder.typicode.com

Without the flag the test fails with a NotCapable error rather than hanging on a prompt. Tests that hit real services are slow and flaky; for unit tests, stub fetch or use the mocking helpers in @std/testing.

The test function receives a context object, t. t.step creates nested sub-tests that are reported separately. Await each step.

import { assertEquals } from "jsr:@std/assert@1";
Deno.test("string helpers", async (t) => {
await t.step("upper-cases", () => {
assertEquals("deno".toUpperCase(), "DENO");
});
await t.step("trims", () => {
assertEquals(" deno ".trim(), "deno");
});
});
Deno.test({
name: "only runs on Windows",
ignore: Deno.build.os !== "windows",
fn() {
assertEquals(Deno.build.os, "windows");
},
});

The object form of Deno.test also takes only: true (run just this test; the run is reported as failed so you don’t commit it by accident) and per-test permissions.

FlagEffect
--filter "divide"Run only tests whose name contains the string; --filter "/regex/" for a pattern
--parallelRun test files in parallel (one worker per CPU by default). Without it, files run one after another
--fail-fastStop after the first failure
--watchRe-run when files change
--coverage[=dir]Collect coverage (default coverage/) and print a summary; deno coverage produces more reports
--reporter=dot|junit|tapChange the output format, e.g. JUnit XML for CI
--docAlso type-check and run code blocks in JSDoc and Markdown

deno test --help lists the rest.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)