Full answer
The assert module throws an AssertionError when a check fails, and a test runner turns that into a failed test.
Legacy mode (require('node:assert')):
- assert.equal(a, b) uses ==. So 1 equals '1', and null equals undefined.
- assert.deepEqual compares objects with loose rules.
Strict mode (require('node:assert/strict'), or assert.strict):
- equal means strictEqual (uses Object.is, so NaN equals NaN and +0 does not equal -0).
- deepEqual means deepStrictEqual: same keys, same values with strict comparison, and same prototype. {a: 1} and an instance of a class with a: 1 are different.
- Failure messages show a readable diff.
Useful helpers:
- assert.ok(value) or assert(value): truthy check.
- assert.throws(fn, expected): the function must throw. expected can be an error class, a regular expression for the message, an object with properties (code, message), or a validation function.
- assert.rejects(promiseOrFn, expected): the async version. Always await it, or the test ends before the check runs.
- assert.doesNotReject, assert.match(string, regexp), assert.notStrictEqual, assert.deepStrictEqual with Maps and Sets.
- AssertionError has actual, expected, operator and code ('ERR_ASSERTION').
Use strict mode in all new tests. Loose comparison hides bugs, for example a function that returns the string '3' instead of the number 3.
const loose = require('node:assert');
const strict = require('node:assert/strict');
function attempt(label, fn) {
try {
fn();
console.log(label.padEnd(44), 'passes');
} catch (err) {
console.log(label.padEnd(44), 'fails with', err.code, '/ operator', err.operator);
}
}
attempt("loose equal(1, '1')", () => loose.equal(1, '1'));
attempt("strictEqual(1, '1')", () => loose.strictEqual(1, '1'));
attempt('loose deepEqual({a: 1}, {a: "1"})', () => loose.deepEqual({ a: 1 }, { a: '1' }));
attempt('deepStrictEqual({a: 1}, {a: "1"})', () => strict.deepEqual({ a: 1 }, { a: '1' }));
class Point { constructor() { this.x = 1; } }
attempt('deepEqual(new Point(), {x: 1}) loose', () => loose.deepEqual(new Point(), { x: 1 }));
attempt('deepEqual(new Point(), {x: 1}) strict', () => strict.deepEqual(new Point(), { x: 1 }));
attempt('strictEqual(NaN, NaN)', () => strict.equal(NaN, NaN));
// Checking errors
strict.throws(() => JSON.parse('{bad'), { name: 'SyntaxError' });
console.log('assert.throws matched a SyntaxError');
async function main() {
await strict.rejects(Promise.reject(new RangeError('marks out of range')), { name: 'RangeError', message: /out of range/ });
console.log('assert.rejects matched the RangeError');
// Forgetting to await would let the test end before this check runs.
await strict.rejects(async () => { throw new TypeError('x'); }, TypeError);
console.log('assert.rejects accepts an async function too');
}
main();The first four rows show how loose and strict comparison differ for the same values. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): loose equal(1, '1') passes strictEqual(1, '1') fails with ERR_ASSERTION / operator strictEqual loose deepEqual({a: 1}, {a: "1"}) passes deepStrictEqual({a: 1}, {a: "1"}) fails with ERR_ASSERTION / operator deepStrictEqual deepEqual(new Point(), {x: 1}) loose passes deepEqual(new Point(), {x: 1}) strict fails with ERR_ASSERTION / operator deepStrictEqual strictEqual(NaN, NaN) passes assert.throws matched a SyntaxError assert.rejects matched the RangeError assert.rejects accepts an async function too
Key points
- Import node:assert/strict so short names are strict.
- deepStrictEqual compares prototypes and uses strict value checks.
- assert.throws and assert.rejects check errors. Await rejects.
- AssertionError exposes actual, expected and operator.
Common mistakes
- Using assert.equal and getting a pass for 1 and '1'.
- Forgetting await before assert.rejects.
- Comparing class instances with plain objects and expecting a pass in strict mode.
- Using assert.throws on an async function, which returns a promise and does not throw.
Follow-ups
- How does deepStrictEqual compare Maps, Sets and Dates?
- What does Object.is do differently from ===?
- How do you assert an error code and message together?
Tips
- Say 'import assert/strict' as the habit.
- Show NaN and prototype cases to prove depth.
- Warn about the missing await on rejects.