partialDeepStrictEqual For Subset Checks

Assert nested config subsets with assert.partialDeepStrictEqual: extra keys pass, a mismatched nested value throws AssertionError with ERR_ASSERTION.

JavascriptConfig Management

Code

snippet
import assert from 'node:assert/strict';

const loaded = { port: 4017, host: 'localhost', retries: 3, tls: { enabled: false } };

// partialDeepStrictEqual checks a subset, nested — extra keys are fine
assert.partialDeepStrictEqual(loaded, { port: 4017, tls: { enabled: false } });
console.log('expected subset present');

// a mismatched nested value throws AssertionError instead of passing silently
try {
  assert.partialDeepStrictEqual(loaded, { tls: { enabled: true } });
  console.log('mismatch accepted');
} catch (err) {
  console.log('rejected:', err.name, err.code);
}

Output

Captured from a sandboxed run of this snippet in the build pipeline.

expected subset present
rejected: AssertionError ERR_ASSERTION

How the code works

Top-level bindings loaded hold the demo state — nothing sits inside a function scope.

What to watch for

  • node:assert/strict resolves inside Node.js without any package — it is a core module.

Applying it

The run emits 3 labeled sections — expected subset present, mismatch accepted, rejected: — one per step.

Statements that matter

const loaded = { port: 4017, host: 'localhost', retries: 3, tls: { enabled: false } };
assert.partialDeepStrictEqual(loaded, { port: 4017, tls: { enabled: false } });
assert.partialDeepStrictEqual(loaded, { tls: { enabled: true } });

Interactive Tools for This Topic

FAQ

What does this import from outside the project?
Only `node:assert/strict`, and that is a Node.js built-in — it comes with the runtime.