Skip to the solution
MediumBackend6 min read

What is the built-in node:test runner and how does it compare to Jest and Mocha?

434 views

The problem

Question presented to candidate: "A small internal service needs tests, but you're hesitant to add Jest or Mocha as dependencies just for that. Is there a real alternative that ships with Node itself, and is it actually production-capable or just a toy?"

What a strong answer should cover:

  • node:test is a built-in test runner, stable since Node 20 β€” genuinely no npm install required at all to write and run real tests, directly answering the prompt's core concern.
  • πŸ“Œ Verified, not assumed: a real .test.js file, run with node --test and zero dependencies installed, genuinely reported 1 pass, 1 fail β€” the deliberately failing test produced a real AssertionError with an actual diff (5 !== 999), and the overall process genuinely exited with a real non-zero (1) exit code β€” directly usable as a real CI pass/fail gate, not merely a toy demonstration.
  • The core API shape is genuinely familiar to anyone who has used Jest or Mocha: test(), describe(), assert (Node's own built-in node:assert/strict, or a custom assertion library if preferred), async test support, before/after hooks β€” the same conceptual shape, not a fundamentally different testing philosophy to learn.
  • πŸ“Œ Verified, not assumed: node:test also ships built-in mocking (mock.fn, t.mock.timers, t.mock.method) with no separate library needed β€” real, directly demonstrated in this bank's dedicated mocking question, including genuinely fast-forwarding a real 10-second timer without any real waiting.
  • The honest, precise scope: node:test genuinely covers the core testing needs (assertions, mocking, async, hooks, a real CI-usable exit code, built-in code coverage via --experimental-test-coverage) β€” but Jest specifically still leads in some areas not built into node:test at all, most notably snapshot testing and a mature ecosystem of framework-specific integrations (React Testing Library's Jest-specific matchers, for instance) β€” a precise answer names this gap rather than claiming feature parity.

Clarifying questions expected:

  • "Does this project need snapshot testing, or framework-specific test matchers that assume Jest specifically?" β€” the most concrete gap where node:test genuinely doesn't yet match Jest's ecosystem.
  • "Is minimizing dependencies (the prompt's stated concern) a hard requirement, or just a mild preference that a well-justified Jest/Mocha addition could still satisfy?" β€” shapes how much weight the zero-install benefit should actually carry.

Code / implementation expected: Yes β€” a real node --test run showing a genuine pass, a genuine failure with a real diff, and a real CI-relevant exit code is the concrete, convincing proof that this is production-capable, not a toy.

  • nodejs
  • testing
  • node-test
  • tooling

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 Β· 7 sections

Read the code

JavaScriptA real node:test file: a genuine pass, a genuine failure with a real diff, and a real non-zero exit code
const test = require("node:test");
const assert = require("node:assert/strict");

function add(a, b) { return a + b; }

test("add() sums two numbers", () => {
  assert.equal(add(2, 3), 5);
});

test("add() genuinely fails on a wrong expectation", () => {
  assert.equal(add(2, 3), 999); // deliberately wrong, to show real failure output
});

// $ node --test math.test.js
// β„Ή pass 1
// β„Ή fail 1
// βœ– add() genuinely fails on a wrong expectation (0.8842ms)
//   AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
//   5 !== 999
//
// $ echo $?
// 1   <- real non-zero exit code, directly CI-gateable

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 91 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 nextMedium

How do you handle uncaught exceptions and unhandled promise rejections in Node.js?

Continue