Skip to content
Tutorials Center
My learning

Topics

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.

NODE
assert.partialDeepStrictEqual(
  { id: 'a1f3', name: 'Maya', roles: ['admin', 'dev'] },
  { name: 'Maya' },
);

Try it

Node.js Objects, arrays, sets and maps

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.

Node.js Ignoring generated fields in a test

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

NODE
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 expected must exist in actual with a deep strictly equal value. Keys that exist only in actual are ignored. An empty expected object matches any object.
  • Arrays are matched in order. Items of expected must appear in actual in 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 in actual can satisfy only one item of expected, so [1, 1] is not contained in [1, 2].
  • Sets and maps are matched by membership: every element (or key and value pair) of expected must be present in actual.
  • Strictness. Primitives are compared with Object.is() semantics, so 0 and -0 differ and NaN equals NaN. Types are never coerced.
  • Prototypes are not compared. A class instance in actual can satisfy a plain object in expected as 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() inside node:test tests, where the assertion is counted for t.plan().