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
assertmodule provides functions that throw anAssertionErrorwhen 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 realAssertionErrorcarrying a detailed, diff-style message showing both values — not a generic, unhelpful error. assert.strictEqual(===semantics) andassert.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. treating1and"1"as equal).assertis 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).
assertrequires no test framework at all — it is directly usable in a plainnodescript 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.