Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท util, assert and the Test Runner

Node.js assert/strict module

Reference for node:assert/strict: the strict mode of the assert module where equal means strictEqual and deepEqual means deepStrictEqual, with better diffs.

node:assert/strict is the same assertion library as node:assert, but with the legacy loose methods replaced by their strict counterparts. In strict mode assert.equal() behaves like assert.strictEqual(), assert.deepEqual() behaves like assert.deepStrictEqual(), and the not variants follow the same rule.

The legacy loose versions use ==, which coerces types and gives surprises such as 1 == '1'. Strict mode removes that whole class of bugs and also produces clearer failure messages, so it is what you should import in new code and tests.

NODE
import assert from 'node:assert/strict';

assert.equal(2 + 2, 4);
assert.deepEqual({ a: [1, 2] }, { a: [1, 2] });

Try it

Node.js Legacy mode versus strict mode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Three ways to get strict mode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
import assert from 'node:assert/strict';
import { strict as assert } from 'node:assert';
const assert = require('node:assert').strict;

All three forms give the same object. The strict object has every method of the regular module: ok, fail, throws, doesNotThrow, rejects, doesNotReject, match, doesNotMatch, ifError, AssertionError, partialDeepStrictEqual and the comparison functions below.

Method in strict mode Same as
assert.equal() assert.strictEqual()
assert.notEqual() assert.notStrictEqual()
assert.deepEqual() assert.deepStrictEqual()
assert.notDeepEqual() assert.notDeepStrictEqual()

Usage notes

  • What changes. Only the four comparison pairs above change. Everything else behaves identically in both modes.
  • Strict equality uses Object.is(). NaN equals NaN, and 0 does not equal -0. This differs from the === operator.
  • Deep strict equality compares prototypes, own enumerable properties (and symbol keys), array holes, and the contents of Map, Set, typed arrays, dates and regular expressions. A plain object does not equal an instance of a class with the same fields.
  • Better messages. Failed object comparisons show a + actual - expected diff, which is the format you want when reading test output.
  • Other spellings. assert.strict on the default export is the same object as the node:assert/strict module, as the second example shows.
  • Migration advice. When converting old tests, replace deepEqual with deepStrictEqual first and fix the tests that start failing: they were hiding type bugs.
  • In node:test. The test context offers t.assert, which has the assertion methods bound to the test so they count for t.plan(). Import node:assert/strict at the top of the file when you prefer the explicit form.
  • Not for browsers. The module is part of Node.js. Bundlers provide polyfills that may lack some newer methods.