Node.js Reference ยท util, assert and the Test Runner
Node.js assert.partialDeepStrictEqual() method
Reference for assert.partialDeepStrictEqual() in Node.js: check that an actual value contains everything in the expected value, with strict types and no exact match.
assert.partialDeepStrictEqual(actual, expected) passes when actual contains at least everything described by expected. Extra properties, extra array items and extra set or map entries in actual are allowed. Every value that expected does mention is compared with the same strict rules as assert.deepStrictEqual(): no type coercion, so 1 and '1' are different.
It is the right tool when a function returns a large object and you only care about a few fields, for example an ID that is generated on each run next to a name you can predict.
assert.partialDeepStrictEqual( { id: 'a1f3', name: 'Maya', roles: ['admin', 'dev'] }, { name: 'Maya' }, );
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.partialDeepStrictEqual(actual, expected[, message])
| Name | Description |
|---|---|
actual |
The value produced by your code. It may contain more than expected. |
expected |
The shape that actual must contain. |
message |
Optional string or Error. A string becomes the start of the message (the diff is still appended); an Error instance is thrown as is. |
Returns: undefined when the check passes.
Throws: an AssertionError (code is 'ERR_ASSERTION', operator is 'partialDeepStrictEqual') when actual does not contain expected.
Usage notes
- Subset semantics. For plain objects, every key in
expectedmust exist inactualwith a deep strictly equal value. Keys that exist only inactualare ignored. An emptyexpectedobject matches any object. - Arrays are matched in order. Items of
expectedmust appear inactualin the same relative order, but not necessarily next to each other:[2, 4]is contained in[1, 2, 3, 4],[4, 2]is not. Each item inactualcan satisfy only one item ofexpected, so[1, 1]is not contained in[1, 2]. - Sets and maps are matched by membership: every element (or key and value pair) of
expectedmust be present inactual. - Strictness. Primitives are compared with
Object.is()semantics, so0and-0differ andNaNequalsNaN. Types are never coerced. - Prototypes are not compared. A class instance in
actualcan satisfy a plain object inexpectedas long as the listed properties match. - Not for "exactly these fields". When extra properties would be a bug, use
assert.deepStrictEqual(). A partial check hides accidental additions. - Version note. Added in Node.js 22.13 and marked experimental at first. Check the stability note of your Node.js version before relying on it in a library that supports older releases.
- It is also available as
t.assert.partialDeepStrictEqual()insidenode:testtests, where the assertion is counted fort.plan().