Typorithm · easy

node:assert

node-assertnodejsassertstrictdeep-equal
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn node:assert

0%

Testing & Debug

node:assert provides strictEqual, deepStrictEqual, throws, rejects, and match. Import node:assert/strict so every method uses strict (===) comparison. Assertions throw an AssertionError on failure, and the strict variants are preferred over the loose ones.

node:assert ships strictEqual and deepStrictEqual for value and deep structural checks, throws and rejects for error expectations, and match for regex checks.

Importing from node:assert/strict (or using the strict variants directly) makes comparisons use === — the loose equal/deepEqual methods coerce and should be avoided.

Every assertion throws an AssertionError when it fails; throws checks a synchronous throw while rejects (awaited) checks a rejected promise.

01

strictEqual / deepStrictEqual / throws / rejects / match.

02

node:assert/strict makes everything strict (===).

03

Failures throw AssertionError; prefer strict over loose.

import assert from "node:assert/strict";

assert.strictEqual(1 + 1, 2);
assert.deepStrictEqual({ a: 1 }, { a: 1 });
assert.throws(() => JSON.parse("{"), SyntaxError);
await assert.rejects(Promise.reject(new Error("x")));
assert.match("hello", /ell/);

Read the implementation

Connect each part of the code to the concept before you type.

1. Strict mode import

Importing node:assert/strict makes every method use strict (===) comparison by default.

import assert from "node:assert/strict";

2. strictEqual

strictEqual compares with === and throws AssertionError if the values differ.

assert.strictEqual(1 + 1, 2);

3. deepStrictEqual

deepStrictEqual recursively compares structure and values using strict equality.

assert.deepStrictEqual({ a: 1 }, { a: 1 });

4. throws

throws asserts the function throws, optionally matching the expected error type.

assert.throws(() => JSON.parse("{"), SyntaxError);

5. rejects (async)

rejects is the async counterpart: await it to assert a promise rejects.

await assert.rejects(Promise.reject(new Error("x")));

6. match

match asserts that a string matches the given regular expression.

assert.match("hello", /ell/);

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

node:assert: Lesson & Practice · Typorithm | ApplyDjinn