Testing (node:test)

A test is code that automatically checks your other code behaves as expected, and Node ships a built-in test runner — node:test — plus an assertion library, node:assert , with nothing to install.

Learn Testing (node:test) in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll write tests with test() and assertions, compare values and objects, test code that throws, group tests with describe / it , handle async tests, and run everything with node --test .

What You'll Learn in This Lesson

1️⃣ Your First Test

A test pairs the test() function from node:test with an assertion from node:assert . The assertion throws if reality doesn't match your expectation; if nothing throws, the test passes. Run the file with node --test .

2️⃣ The Everyday Assertions

Different checks call for different assertions: strictEqual for primitives, deepStrictEqual to compare objects and arrays by value, ok for truthiness, and throws to assert that code raises an error.

The trap to remember: strictEqual on two separate objects with identical contents fails, because === compares references. Use deepStrictEqual for structured data.

3️⃣ Grouping and Async Tests

Use describe() to group related cases under a heading and it() to name each one. Tests can be async : make the function async and await inside — the runner waits before judging.

Your turn. Fill in the two ___ blanks to complete the tests for isEven , then run them.

Write three tests for the total() function — empty cart, one line, two lines — grouped with describe and it . Run them with node --test .

📋 Quick Reference — node:test

Practice quiz

Which built-in modules let you write tests in modern Node with no install?

  • node:jest and node:mocha
  • node:test and node:assert
  • node:check and node:spec
  • node:runner and node:expect

Answer: node:test and node:assert. node:test is the runner and node:assert provides the assertions — both built in.

Which assertion compares objects and arrays by their contents?

  • assert.deepStrictEqual
  • assert.strictEqual
  • assert.ok
  • assert.equalRef

Answer: assert.deepStrictEqual. deepStrictEqual recurses through properties; strictEqual uses === (reference).

Why does assert.strictEqual({a:1}, {a:1}) fail?

  • The syntax is wrong
  • Objects aren't allowed
  • It uses === which compares references, not contents
  • It needs await

Answer: It uses === which compares references, not contents. strictEqual is reference equality; two separate objects are never ===.

How do you assert that a function throws an error?

  • assert.fail(fn)
  • assert.error(fn)
  • assert.catch(fn)
  • assert.throws(() => fn())

Answer: assert.throws(() => fn()). Pass a function to assert.throws; it passes if that function throws.

What command runs your tests with the built-in runner?

  • node --test
  • npm test
  • node run tests
  • node:test start

Answer: node --test. node --test finds test files and reports pass/fail counts.

What do describe() and it() do?

  • They mock network calls
  • They group related tests and name each case
  • They run only failing tests
  • They time the tests

Answer: They group related tests and name each case. describe() groups tests under a heading; it() defines a single named case.

How do you correctly test asynchronous code?

  • Use setTimeout to wait
  • Ignore the promise
  • Make the test async and await the operation
  • Call done() twice

Answer: Make the test async and await the operation. An async test function lets the runner await the promise before judging pass/fail.

Which assertion checks that a value is truthy?

  • assert.true
  • assert.is
  • assert.truthy
  • assert.ok

Answer: assert.ok. assert.ok(value) passes when value is truthy.

Where does the runner look for test files by default?

  • Files like *.test.js or inside a test/ folder
  • Only in src/
  • Anywhere named main.js
  • Only files you pass with --watch

Answer: Files like *.test.js or inside a test/ folder. node --test auto-discovers *.test.js files and a test/ directory.

What happens if you forget to await an async assertion?

  • The test always fails
  • Node refuses to run
  • The test may finish early and a real failure slips through
  • It retries forever

Answer: The test may finish early and a real failure slips through. Without await/return the test ends before the work, hiding real failures.