assert.deepStrictEqual Compares Nested Data

Compare parsed data against expected structures with assert.deepStrictEqual, where object key order is ignored but array order matters.

JavascriptData Integrity

Code

javascriptsnippet
19 lines
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 rejected

Inside 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:assert is 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

FAQ

Are there third-party dependencies?
No. The imports resolve to Node.js core modules, which the runtime provides.