Testing JavaScript with node:test: no framework needed
Write and run JavaScript tests with Node.js's built-in test runner: test and describe, assert.strictEqual vs deepStrictEqual, async tests, mocks and what makes a good test.
Tests let you change code without fear. Node.js has had a built-in test runner since version 18, so you can start testing without installing anything.
Your first test
// pricing.js
export function seatPrice(age) {
if (age < 12) return 6;
if (age >= 65) return 8;
return 11;
}
// pricing.test.js
import { test } from "node:test";
import assert from "node:assert/strict";
import { seatPrice } from "./pricing.js";
test("children pay the child price", () => {
assert.equal(seatPrice(8), 6);
});
test("the senior price starts at 65", () => {
assert.equal(seatPrice(64), 11);
assert.equal(seatPrice(65), 8);
});Run every test file with node --test. Node.js finds files named *.test.js (and a few other
patterns) automatically.
Choosing the right assertion
Import from node:assert/strict so the basic assertions compare strictly:
| Assertion | Passes when |
|---|---|
assert.equal(a, b) | a === b |
assert.deepEqual(a, b) | objects/arrays have the same contents |
assert.ok(value) | value is truthy |
assert.throws(fn, /message/) | fn() throws an error matching the pattern |
await assert.rejects(promise) | the promise rejects |
assert.deepEqual(groupByFilm(tickets), { Dune: 3, Heat: 4 });
assert.throws(() => seatPrice(-1), /age must be positive/);Grouping and async tests
import { describe, it } from "node:test";
describe("booking", () => {
it("reserves free seats", async () => {
const booking = await reserve("A1");
assert.equal(booking.seat, "A1");
});
it("rejects a taken seat", async () => {
await assert.rejects(reserve("A1"), /already taken/);
});
});Make async tests async and await inside them; otherwise the test can finish before the work
does and pass by accident.
Mocks: replacing the slow or unpredictable parts
node:test includes mock for functions and timers:
import { mock, test } from "node:test";
test("retries a failing request once", async () => {
const send = mock.fn(() => Promise.resolve("ok"));
// The first call fails; later calls use the default implementation above.
send.mock.mockImplementationOnce(() => Promise.reject(new Error("timeout")));
assert.equal(await withRetry(send), "ok");
assert.equal(send.mock.callCount(), 2);
});Mock the boundary (network, clock, randomness), not your own logic. Code that takes its dependencies as parameters is much easier to test this way.
What makes a good test
- One behaviour per test, named as a sentence: "the senior price starts at 65".
- Boundaries: test 64 and 65, empty lists, the last seat.
- Independent: no test relies on another having run first.
- Fast: milliseconds, so you run them all the time.
node --test --experimental-test-coverage shows which lines your tests never reach.
The projects in the JavaScript Developer career path are checked by
node:test suites just like these.
Questions and answers
- Do I need Jest or Vitest to test JavaScript?
- Not to start. Node.js 18+ ships node:test and node:assert, which cover test cases, groups, async tests, mocks and coverage. Bigger frameworks add conveniences like watch mode and snapshot tests, but the ideas are the same.
- What is the difference between strictEqual and deepStrictEqual?
- strictEqual compares with ===, so two different objects with the same contents are not equal. deepStrictEqual compares the contents of objects and arrays recursively.
Practise it
Read next
- JavaScript modules: import, export and ES modules in Node.js
Named versus default exports, how ES modules differ from CommonJS require, setting up Node.js for import, and how to organise a project into small modules.
- JavaScript promises and async/await: a practical guide
How promises work, what await really waits for, running tasks in parallel with Promise.all, handling errors properly, and the event loop in two minutes.
- Code review checklist for junior developers
What senior engineers look for in a code review, as a practical checklist: correctness, edge cases, tests, naming, structure and performance, with C++-specific points.