Node test runner and assertions
Prerequisite: node-graceful-process-supervision. Objective: Test pure order function. Local baseline: Node.js 22.16.0 on Linux, ESM. Each example is independent: save as main.mjs in a disposable directory and run node main.mjs. The author harness uses a five-second timeout. Static snippets are not ByteMind interactive execution.
How it works
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.
Test an order total with a table of inputs
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');Expected output:
3 assertions passedEach row is a separate input/output contract. The zero case catches implementations that add a fixed fee.
Run actual node:test and check the exit status
import {spawnSync} from 'node:child_process';import {writeFileSync,mkdtempSync,rmSync} from 'node:fs';import {tmpdir} from 'node:os';import {join} from 'node:path';const dir=mkdtempSync(join(tmpdir(),'bm-test-'));try { const path=join(dir,'order.test.mjs'); const run=expected=>{writeFileSync(path,`import test from 'node:test';import assert from 'node:assert/strict';test('total',()=>assert.equal(2*350,${expected}));`);return spawnSync(process.execPath,['--test',path],{encoding:'utf8',timeout:2000,env:{PATH:process.env.PATH}});}; console.log('correct',run(700).status);console.log('incorrect',run(701).status);}finally{rmSync(dir,{recursive:true,force:true});}Expected output:
correct 0incorrect 1A failing assertion produces a failing runner exit status. The child report is intentionally not printed.
Common mistakes
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.
Practice
Add a zero-quantity case and verify that a version using addition instead of multiplication fails.
Reference candidate: node-node-test-runner-and-assertions-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/test.html. Selected versioned documentation opened.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Node test runner and assertions — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What should a test of total(0,350) assert?
How should an async rejection assertion be used?
Which result should CI inspect after running node --test?