typestar

Async tests in JavaScript

Return a promise or take a callback; the runner waits either way.

import test from "node:test";
import assert from "node:assert/strict";

async function load(lang) {
  if (!lang) throw new RangeError("lang is required");
  return { lang, steps: 45 };
}

test("resolves with the tour", async () => {
  const tour = await load("javascript");
  assert.deepStrictEqual(tour, { lang: "javascript", steps: 45 });
});

test("rejects without a language", async () => {
  await assert.rejects(() => load(""), {
    name: "RangeError",
    message: /required/,
  });
});

How it works

  1. An async test function is awaited.
  2. assert.rejects is the promise form of assert.throws.
  3. A test that forgets to await passes for the wrong reason.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
474
Tokens
114
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 65 seconds.

Type this snippet

Step 1 of 2 in Async & hooks, step 3 of 9 in Testing with node:test.

← Previous Next →