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.
import assert from 'node:assert/strict'; assert.equal(2 + 2, 4); assert.deepEqual({ a: [1, 2] }, { a: [1, 2] });
Try it
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.
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
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().NaNequalsNaN, and0does 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 - expecteddiff, which is the format you want when reading test output. - Other spellings.
assert.stricton the default export is the same object as thenode:assert/strictmodule, as the second example shows. - Migration advice. When converting old tests, replace
deepEqualwithdeepStrictEqualfirst and fix the tests that start failing: they were hiding type bugs. - In
node:test. The test context offerst.assert, which has the assertion methods bound to the test so they count fort.plan(). Importnode:assert/strictat 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.