Node test runner and assertions
A unit test checks a small behavior in isolation. Node ships a test runner in `node:test` and strict assertions in `node:assert/strict`. A test should fail when its promised behavior is violated, not when a private implementation changes. Arrange inputs, act by calling the function, and assert the result. For a function that returns a promise, return or await that promise inside the test callback. A synchronous exception can be checked with `assert.throws`; an asynchronous rejection needs `await assert.rejects`. Use `node --test file.mjs` for ordinary test reports. The fixtures here use programmatic assertions and a spawned test runner with its report summarized so output is reproducible. Compare Jest and Vitest only if their dependencies and tooling are needed; neither is required for built-in tests. Do not forget to await a rejection assertion or test only the happy path. A test that finishes before its assertion runs can conceal a bug. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/test.html.
Formal Syntax & Structure
test(name, fn); assert.strictEqual(actual, expected)Working Example
import assert from 'node:assert/strict';function total(qty, cents) { return qty * cents; }for (const [q,p,expected] of [[2,350,700],[0,350,0],[1,1,1]]) { assert.equal(total(q,p),expected);}console.log('3 assertions passed');