typestar

node:test in JavaScript

The runner is built in: a test function and the assert module.

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

function slugify(title) {
  return title.trim().toLowerCase().replaceAll(" ", "-");
}

test("joins words with hyphens", () => {
  assert.strictEqual(slugify("Language Basics"), "language-basics");
});

test("trims the edges", () => {
  assert.equal(slugify("  Async  "), "async");
});

test("leaves one word alone", () => {
  assert.ok(!slugify("basics").includes("-"));
});

How it works

  1. test(name, fn) registers it; the runner reports each one.
  2. assert.strictEqual is the default comparison.
  3. Run it with node --test or by running the file.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
438
Tokens
108
Three-star pace
100 tpm

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

Type this snippet

Step 1 of 2 in Writing tests, step 1 of 9 in Testing with node:test.

Next →