assert.deepStrictEqual Compares Nested Data
Compare parsed data against expected structures with assert.deepStrictEqual, where object key order is ignored but array order matters.
Code
import { deepStrictEqual } from 'node:assert';
// deepStrictEqual compares structure, not identity, and checks prototypes
const restored = JSON.parse('{"id":7,"tags":["cache","slow"],"meta":{"tries":2}}');
const expected = { id: 7, tags: ['cache', 'slow'], meta: { tries: 2 } };
deepStrictEqual(restored, expected);
console.log('restored matches expected:', true);
// object key order does not matter
deepStrictEqual({ a: 1, b: 2 }, { b: 2, a: 1 });
console.log('key order ignored:', true);
// array order does matter
try {
deepStrictEqual([1, 2], [2, 1]);
} catch {
console.log('array order differences rejected');
}Output
Captured from a sandboxed run of this snippet in the build pipeline.
restored matches expected: true
key order ignored: true
array order differences rejectedInside the snippet
deepStrictEqual comes from node:assert — Node.js core modules the runtime ships with.
The demo data lives in module-level bindings: restored, expected.
Caveats
node:assertis a Node.js built-in module — it ships with the runtime, so there is nothing to install.
Using it
Look for the restored matches expected:, key order ignored:, array order differences rejected prefixes when the file runs; they tie each line to its step.
Key statements
const restored = JSON.parse('{"id":7,"tags":["cache","slow"],"meta":{"tries":2}}');
const expected = { id: 7, tags: ['cache', 'slow'], meta: { tries: 2 } };
deepStrictEqual({ a: 1, b: 2 }, { b: 2, a: 1 });
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.JSONPath TesterTest JSONPath expressions against JSON documents with dot notation, brackets, wildcards, and recursive descent.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Are there third-party dependencies?
- No. The imports resolve to Node.js core modules, which the runtime provides.
Report a problem with this exampleOpens a pre-filled issue on GitHub.