Skip to content
Tutorials Center
My learning

Topics

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

Node.js assert.strictEqual() method

Reference for assert.strictEqual() in Node.js: compare with Object.is semantics, read the failure diff, and know how it differs from loose assert.equal().

assert.strictEqual(actual, expected) passes when the two values are identical according to Object.is(), and throws an AssertionError otherwise. It is the everyday assertion for primitives such as numbers, strings and booleans, and for checking that two variables point to the very same object.

It does not look inside objects. Two separate objects with the same contents are not strictly equal; use assert.deepStrictEqual() for that. The failure message shows both values, so you see what you got and what you wanted.

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

assert.strictEqual(1 + 1, 2);
assert.strictEqual("a" + "b", "ab");

Try it

Node.js What counts as equal

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 Reading the failure and the loose version

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
assert.strictEqual(actual: unknown, expected: unknown, message?: string | Error): void
assert.notStrictEqual(actual: unknown, expected: unknown, message?: string | Error): void
Name Description
actual The value your code produced.
expected The value you expect.
message Optional text, or an Error that is thrown instead of the default one.

Returns: undefined.

Errors: an AssertionError with operator "strictEqual" (or "notStrictEqual"). Its actual and expected properties hold the two values. With a custom message, generatedMessage is false.

Usage notes

  • The comparison is Object.is(): NaN equals NaN, but 0 does not equal -0. This differs from === in exactly those two cases.
  • No type conversion happens. 1 and "1" are different.
  • Put the value from your code first and the literal you expect second, so the diff reads "actual versus expected" correctly.
  • For long multi-line strings, the failure message prints a line diff, which makes small differences easy to spot.
  • In node:assert/strict, assert.equal is an alias of assert.strictEqual. In the legacy node:assert module equal() uses == and hides bugs, so always import the strict module.
  • Objects and arrays compare by reference. A custom message of Values have same structure but are not reference-equal tells you to switch to deepStrictEqual.