QA
Beginner LevelNode JS

How do you write zero-dependency assertions using the node:assert module?

❓ Interview Prompt: Explain `node:assert` module, strict mode (`assert.strict`), `strictEqual`, `deepStrictEqual`, and `rejects`.

The native 'node:assert' module provides zero-dependency assertion testing capabilities:

•
'assert.strictEqual(actual, expected)': Checks equality with '==='.
•
'assert.deepStrictEqual(a, b)': Recursively checks object/array structure equality.
•
'assert.rejects(promise)': Asserts that an asynchronous Promise rejects.

Code Implementation Example

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

// Strict equality assertion
assert.strictEqual(2 + 2, 4);

// Deep object structural assertion
assert.deepStrictEqual(
  { user: 'Alice', roles: ['admin'] },
  { user: 'Alice', roles: ['admin'] }
);

console.log('All assertions passed!');
💡Writing strict unit test assertions using node:assert/strict.
🎯 Interview Key Takeaway

Use node:assert/strict for zero-dependency unit testing assertions in Node.js.

#assert#Testing#Unit Tests#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?