Node.js Reference ยท util, assert and the Test Runner
Node.js assert.deepStrictEqual() method
Reference for assert.deepStrictEqual() in Node.js: compare objects, arrays, Maps and Sets by content, with prototype checks, edge cases and readable diffs.
assert.deepStrictEqual(actual, expected) compares two values by their contents, however deeply they are nested, and throws an AssertionError with a diff when they differ. It is the standard way to check arrays, plain objects, Maps, Sets, dates and other structured results in a test.
"Strict" means that primitives are compared like Object.is(), prototypes must match, and own enumerable properties (including symbol keys) are compared. So { a: 1 } does not equal { a: "1" }, and an instance of a class does not equal a plain object with the same fields.
import assert from "node:assert/strict"; assert.deepStrictEqual({ 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
assert.deepStrictEqual(actual: unknown, expected: unknown, message?: string | Error): void assert.notDeepStrictEqual(actual: unknown, expected: unknown, message?: string | Error): void
| Name | Description |
|---|---|
actual |
The value your code produced. |
expected |
The value you want. |
message |
Optional text, or an Error thrown instead of the default. |
Returns: undefined.
Errors: an AssertionError with operator "deepStrictEqual" and a message that marks + actual lines and - expected lines. notDeepStrictEqual throws when the values are equal.
Usage notes
- Own enumerable properties are compared; the order of keys does not matter. For
SetandMapthe insertion order does not matter either. - Prototypes are compared with
Object.getPrototypeOf(), so a class instance never equals a plain object. Build the expected value with the same class, or compare{ ...instance }. { a: 1, b: undefined }is different from{ a: 1 }, and an array hole differs from anundefinedelement.- Errors are compared by
messageandnameas well as their enumerable properties, so comparing two errors usually works. - Circular references are handled. Functions compare by reference, so two separate but identical functions are different.
- Use
assert.partialDeepStrictEqual()when only some properties matter. - For very large structures, the diff output can be long. Compare a summary or use a smaller sample.