javascript / expert
Snippet
Test Execution Planning and Structural Assertion in node:test
t.plan() enforces exact expected assertion counts within test blocks, while assert.partialDeepStrictEqual() verifies complex nested object subsets without breaking when additional properties exist.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import test from 'node:test';import assert from 'node:assert/strict';test('structural response payload validation', (t) => {t.plan(2);const payload = { id: 101, status: 'active', metadata: { retries: 3 } };assert.partialDeepStrictEqual(payload, {status: 'active',metadata: { retries: 3 }});t.assert.ok(true);});
nodejs
Breakdown
1
t.plan(2);
Registers an explicit expectation that exactly two assertions must execute during the test run.
2
assert.partialDeepStrictEqual(payload, { ... });
Validates that the target object contains matching subset properties deeply without demanding full strict object match.
3
t.assert.ok(true);
Executes the second assertion bound to the test context to satisfy the planned execution count.