M
assert.partialDeepStrictEqual
History
Added in: v23.4.0, v22.13.0
v23.4.0, v22.13.0
Introduced in: v0.1.21
v0.1.21
Promises are not considered equal anymore if they are not of the same instance.
v25.0.0
Invalid dates are now considered equal.
v25.0.0
partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
v24.0.0, v22.17.0
assert.partialDeepStrictEqual(actual, expected, message?): void
Attributes
Tests for partial deep equality between the actual and expected parameters.
"Deep" equality means that the enumerable "own" properties of child objects
are recursively evaluated also by the following rules. "Partial" equality means
that only properties that exist on the expected parameter are going to be
compared.
This method always passes the same test cases as assert.deepStrictEqual(),
behaving as a super set of it.
- Primitive values are compared using
Object.is(). - Type tags of objects should be the same.
[[Prototype]]of objects are not compared.- Only enumerable "own" properties are considered.
Errornames, messages, causes, and errors are always compared, even if these are not enumerable properties.errorsis also compared.- Enumerable own
Symbolproperties are compared as well. - 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 both sides encounter a circular reference.
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.- Holes in sparse arrays are ignored.
import assert from 'node:assert'; assert.partialDeepStrictEqual( { a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }, ); // OK assert.partialDeepStrictEqual( { a: 1, b: 2, c: 3 }, { b: 2 }, ); // OK assert.partialDeepStrictEqual( [1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 8], ); // OK assert.partialDeepStrictEqual( new Set([{ a: 1 }, { b: 1 }]), new Set([{ a: 1 }]), ); // OK assert.partialDeepStrictEqual( new Map([['key1', 'value1'], ['key2', 'value2']]), new Map([['key2', 'value2']]), ); // OK assert.partialDeepStrictEqual(123n, 123n); // OK assert.partialDeepStrictEqual( [1, 2, 3, 4, 5, 6, 7, 8, 9], [5, 4, 8], ); // AssertionError assert.partialDeepStrictEqual( { a: 1 }, { a: 1, b: 2 }, ); // AssertionError assert.partialDeepStrictEqual( { a: { b: 2 } }, { a: { b: '2' } }, ); // AssertionError
const assert = require('node:assert'); assert.partialDeepStrictEqual( { a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }, ); // OK assert.partialDeepStrictEqual( { a: 1, b: 2, c: 3 }, { b: 2 }, ); // OK assert.partialDeepStrictEqual( [1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 8], ); // OK assert.partialDeepStrictEqual( new Set([{ a: 1 }, { b: 1 }]), new Set([{ a: 1 }]), ); // OK assert.partialDeepStrictEqual( new Map([['key1', 'value1'], ['key2', 'value2']]), new Map([['key2', 'value2']]), ); // OK assert.partialDeepStrictEqual(123n, 123n); // OK assert.partialDeepStrictEqual( [1, 2, 3, 4, 5, 6, 7, 8, 9], [5, 4, 8], ); // AssertionError assert.partialDeepStrictEqual( { a: 1 }, { a: 1, b: 2 }, ); // AssertionError assert.partialDeepStrictEqual( { a: { b: 2 } }, { a: { b: '2' } }, ); // AssertionError