assert.deepEqual
History
printf-like format string or function.Error names and messages are now properly compared.Set and Map content is also compared.Uint8Array typed arrays correctly.assert.deepEqual(actual, expected, message?): void
Strict assertion mode
An alias of assert.deepStrictEqual().
Legacy assertion mode
assert.deepStrictEqual() instead.Tests for deep equality between the actual and expected parameters. Consider
using assert.deepStrictEqual() instead. assert.deepEqual() can have
surprising results.
Deep equality means that the enumerable "own" properties of child objects are also recursively evaluated by the following rules.
- Primitive values are compared with the
==operator, except forNaN, which is treated as identical when both sides areNaN. - Type tags of objects should be the same.
- Only enumerable "own" properties are considered.
- Object constructors are compared when available.
Errornames, messages, causes, and errors are always compared, even if these are not enumerable properties.- Object wrappers are compared both as objects and unwrapped values.
Objectproperties are compared unordered.Mapkeys andSetitems are compared unordered.- Recursion stops when both sides differ or either side encounters a circular reference.
- Implementation does not test the
[[Prototype]]of objects. Symbolproperties are not compared.WeakMap,WeakSetandPromiseinstances are not compared structurally. They are only equal if they reference the same object. Any comparison between differentWeakMap,WeakSet, orPromiseinstances will result in inequality, even if they contain the same content.RegExplastIndex, flags, and source are always compared, even if these are not enumerable properties.
The following example does not throw an AssertionError because the
primitives are compared using the == operator.
import assert from 'node:assert'; // WARNING: This does not throw an AssertionError! assert.deepEqual('+00000000', false);
const assert = require('node:assert'); // WARNING: This does not throw an AssertionError! assert.deepEqual('+00000000', false);
"Deep" equality means that the enumerable "own" properties of child objects are evaluated also:
import assert from 'node:assert'; const obj1 = { a: { b: 1, }, }; const obj2 = { a: { b: 2, }, }; const obj3 = { a: { b: 1, }, }; const obj4 = { __proto__: obj1 }; assert.deepEqual(obj1, obj1); // OK // Values of b are different: assert.deepEqual(obj1, obj2); // AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } } assert.deepEqual(obj1, obj3); // OK // Prototypes are ignored: assert.deepEqual(obj1, obj4); // AssertionError: { a: { b: 1 } } deepEqual {}
const assert = require('node:assert'); const obj1 = { a: { b: 1, }, }; const obj2 = { a: { b: 2, }, }; const obj3 = { a: { b: 1, }, }; const obj4 = { __proto__: obj1 }; assert.deepEqual(obj1, obj1); // OK // Values of b are different: assert.deepEqual(obj1, obj2); // AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } } assert.deepEqual(obj1, obj3); // OK // Prototypes are ignored: assert.deepEqual(obj1, obj4); // AssertionError: { a: { b: 1 } } deepEqual {}
If the values are not equal, an AssertionError is thrown with a message
property set equal to the value of the message parameter. If the message
parameter is undefined, a default error message is assigned. If the message
parameter is an instance of Error then it will be thrown instead of the
AssertionError.