partialDeepStrictEqual For Subset Checks
Assert nested config subsets with assert.partialDeepStrictEqual: extra keys pass, a mismatched nested value throws AssertionError with ERR_ASSERTION.
Code
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_ASSERTIONHow the code works
Top-level bindings loaded hold the demo state — nothing sits inside a function scope.
What to watch for
node:assert/strictresolves 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
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON to TOML ConverterTurn a JSON object into TOML, emitting nested objects as tables and arrays of objects as array-of-tables blocks.JSON to YAML ConverterConvert JSON to readable YAML with nested block mappings, sequences, and quoting only where it is required.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.