Tests with assert
node:assert/strict
A test is code that runs other code and fails loudly when reality differs from the plan. The node:assert/strict module ships with Node, so no install step stands between the reader and the first green run. Import it once at the top of the test file and call its functions directly:
import assert from "node:assert/strict"; function add(a: number, b: number): number { return a + b; } assert.equal(add(2, 3), 5); console.log("green");
green
When the values match, the file runs quietly and prints green. When they differ, equal throws an error that shows both sides, and Node exits before the last line ever runs.
deepStrictEqual
equal uses strict equality, so it fits numbers and strings but not objects. Two object literals are never the same reference, even when every field matches. deepStrictEqual walks both structures and compares every field instead, which is exactly what object tests need:
import assert from "node:assert/strict"; const point = { x: 1, y: 2 }; assert.deepStrictEqual(point, { x: 1, y: 2 }); console.log("deep equal: same shape and values");
deep equal: same shape and values
This check passes because both sides hold the same shape and the same values. Change one field and the error prints the expected and actual objects, so the difference is visible at once.
Check throws
Some code must fail, and a test can demand that failure. assert.throws runs a function and passes only when that function throws. Add a pattern to check the message as well, so the wrong error cannot sneak through:
import assert from "node:assert/strict"; function fail(): never { throw new Error("boom"); } assert.throws(fail, /boom/); console.log("throws: boom");
throws: boom
When the function needs arguments, wrap the call in an arrow function so throws controls the moment of the call. A function that returns normally fails the test, which is the correct verdict for code that should have blown up.
One assert per test
One assert per test keeps failures obvious. A test with five checks stops at the first failure, hides the rest, and points at a crowd. Small focused checks with plain names read like a list of promises. For background on full test frameworks, read the mocha testing background article:
import assert from "node:assert/strict"; function double(n: number): number { return n * 2; } function check(name: string, fn: () => void): void { fn(); console.log("pass: " + name); } check("doubles two", () => { assert.equal(double(2), 4); }); check("doubles zero", () => { assert.equal(double(0), 0); });
pass: doubles two
pass: doubles zero
Each check carries one assert and one name. A failure stops the run inside the check that broke, and the assertion error shows the expected and actual values, so the fix location is never in doubt.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
equalfromnode:assert/strictcompare?Answer
Strict equality, so a string never equals a number
The strict module keeps
equalon strict equality, so"5"and5are different values and the check fails. -
Why does an object test use
deepStrictEqualinstead ofequal?Answer
Two object literals with the same fields are still different references
equalasks whether both sides are the same value or reference, anddeepStrictEqualwalks the fields of both objects instead. -
When does
assert.throws(fail, /boom/)pass?Answer
When
failthrows an error whose message matchesboomThe function must throw, and the regular expression checks the message, so the wrong error cannot pass the test.
-
Why does this lesson keep one assert per named check?
Answer
The first failure hides the rest, so a small named check keeps the report clear
A run stops at the first broken assert, so one assert per check makes every failure point at a single promise.
-
What happens when this program runs?
import assert from "node:assert/strict"; assert.equal({ id: 1 }, { id: 1 }); console.log("same");Answer
It throws an AssertionError, because two object literals are different references
equalcompares references, and each literal creates a new object, so the assert throws before the log line runs. -
What does this program print?
import assert from "node:assert/strict"; function add(a: number, b: number): number { return a + b; } assert.equal(add(2, 3), 5); console.log("green");Answer
green
The sum is five, the assert passes quietly, and the next line prints
green. -
What happens when this program runs?
import assert from "node:assert/strict"; const point = { x: 1, y: 2 }; assert.deepStrictEqual(point, { x: 1, y: 3 }); console.log("pass");Answer
It throws an AssertionError, because the y values differ
deepStrictEqualfinds the mismatched field, so the assert throws andpassnever prints. -
What does this program print?
import assert from "node:assert/strict"; function fail(): never { throw new Error("boom"); } assert.throws(fail, /boom/); console.log("caught");Answer
caught
throwscatches the error and checks its message, so the test continues and printscaught. -
What does the compiler say about this program?
function double(n: number): string { return n * 2; } console.log(double(2));Answer
error TS2322: Type 'number' is not assignable to type 'string'.
The multiplication gives a number while the return type promises a string, so the returned expression is rejected.
-
What does this program print?
import assert from "node:assert/strict"; function double(n: number): number { return n * 2; } function check(name: string, fn: () => void): void { fn(); console.log("pass: " + name); } check("doubles two", () => { assert.equal(double(2), 4); }); check("doubles zero", () => { assert.equal(double(0), 0); });Answer
pass: doubles two / pass: doubles zero
Each check runs one passing assert and then prints its own name, in the order the two calls appear.
Article author: Arthur Isaev