Skip to the solution
EasyPhone Screen6 min read

What is the purpose of the assert module in Node.js?

510 views

The problem

Question presented to candidate: "You see assert.strictEqual(result, expected) in a test file. What actually happens if result does not equal expected, versus if it does?"

What a strong answer should cover:

  • Node's built-in assert module provides functions that throw an AssertionError when a given condition is false, and do nothing at all (no return value printed, no side effect) when the condition is true β€” it is a fail loudly, succeed silently primitive.
  • πŸ“Œ A concrete, verifiable behavior: assert.strictEqual(1, 2) throws a real AssertionError carrying a detailed, diff-style message showing both values β€” not a generic, unhelpful error.
  • assert.strictEqual (=== semantics) and assert.deepStrictEqual (recursive structural equality for objects/arrays, using === for each leaf value) are the two most commonly used functions β€” the "strict" variants are almost always preferred over the older, loose (==-based) assert.equal/assert.deepEqual, since loose comparison can mask real bugs (e.g. treating 1 and "1" as equal).
  • assert is genuinely used in two different contexts: as the low-level assertion primitive underlying test frameworks (Jest, Mocha's assertion libraries often wrap or resemble it), and directly as runtime invariant-checking in application code β€” asserting an internal precondition that should never be false if the code is correct, deliberately crashing loudly if it somehow is.
  • A precise answer distinguishes assert-module-style assertions (a programmer error signal β€” see the dedicated operational-vs-programmer-errors question β€” since a failed invariant means the code's own logic is wrong) from ordinary application error handling (validating genuinely possible external input, which should be handled gracefully, not asserted).
  • assert requires no test framework at all β€” it is directly usable in a plain node script with no dependency, which is a real, practical reason it is still reached for even in a codebase using a full test runner elsewhere.

Clarifying questions expected:

  • "Is this being used inside a test file, or as a runtime invariant check in application code?" β€” both are legitimate, but the framing of "why assert here" differs.
  • "Strict or loose comparison β€” does the distinction matter for this specific check?"

Code / implementation expected: Yes β€” showing both the silent-success and the actual thrown-error-with-message cases side by side is the concrete, convincing part of the answer.

  • testing
  • modules
  • assert

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 10 sections

Read the code

JavaScriptassert's silent success and detailed thrown-error failure, verified for both strictEqual and deepStrictEqual
const assert = require("assert");

assert.strictEqual(1, 1);
console.log("passed silently, no output above this line");

try {
  assert.strictEqual(1, 2);
} catch (e) {
  console.log(e.constructor.name, "-", e.message);
  // AssertionError - Expected values to be strictly equal:
  // 1 !== 2
}

assert.deepStrictEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } });
console.log("deepStrictEqual on equal nested objects passed silently");

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 19 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

How do you replicate __dirname in ES Modules using import.meta.url?

Continue