Node.js theorytheory 0/50 · 0%
Testing · medium

22. Testing with node:test

Node's built-in test runner — no extra dependency required.

Since Node 18+, `node:test` provides a built-in test runner with `test()`, `describe()`/`it()`, and an `assert` module, runnable via `node --test`.

js
import { test } from "node:test";
import assert from "node:assert/strict";

function add(a, b) { return a + b; }

test("add sums two numbers", () => {
  assert.equal(add(2, 3), 5);
});

test("add handles negatives", () => {
  assert.equal(add(-1, 1), 0);
});

Running `node --test` auto-discovers files matching common test naming patterns (`*.test.js`) and reports pass/fail with a TAP-compatible summary — useful for small projects that want tests without adding Jest or Vitest as a dependency.

Check your understanding

  1. 1. What command runs Node's built-in tests?

  2. 2. Which module provides assertion helpers alongside node:test?

  3. 3. What is an advantage of node:test over adding Jest?