DEV Community

Mohammad Taha Azizi Ahad
Mohammad Taha Azizi Ahad

Posted on

Node's Built-in Test Runner Finally Grew Up — Stop Installing a Test Framework for Small Projects

Every new Node project I start follows the same ritual. Scaffold, install a test framework, install an assertion library, install a mocking library, wire up a config file, argue with the config file, and then — finally — write the first test. For a big project, that is worth it. For a small library, a CLI tool, or a side project with three modules, it is pure overhead, and I have been paying it out of habit.

Node's built-in test runner, node:test, has quietly become good enough that the habit is no longer justified. It stabilized in Node 22, it is solid in the current LTS line, and it covers the 80% case with zero dependencies. If you have not looked at it since it was experimental, you are working from old information.

What you actually get for free

A test file is just a file that imports node:test and node:assert. No config, no build step, no plugin system to learn.

// math.test.js
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { add, divide } from './math.js';

test('add returns the sum of two numbers', () => {
  assert.equal(add(2, 3), 5);
});

test('divide throws on division by zero', () => {
  assert.throws(() => divide(1, 0), /division by zero/);
});
Enter fullscreen mode Exit fullscreen mode

Run it with node --test. That is the whole workflow. Node finds your test files, runs them, and prints a readable summary. Pass a path to run one file. Pass --test-name-pattern="add" to run only the tests whose names match.

The runner also handles the things people assume require a framework. Tests run in separate processes by default, so a test that mutates global state cannot silently poison the next file. You get before, after, beforeEach, and afterEach hooks. You get subtests for grouping, test.skip for the test you mean to fix later, and test.only for the one test you are actually debugging right now. Timeouts are configurable per test, and a test that never finishes fails loudly instead of hanging your suite forever.

Mocking without a library

The objection I hear most often is mocking. Fair — for years, node:test had no answer, and reaching for a library was the only option. That changed with the mock module.

import { test, mock } from 'node:test';
import assert from 'node:assert/strict';
import { fetchUserName } from './user.js';

test('fetchUserName returns the name from the service', async () => {
  const fakeFetch = mock.fn(async () => ({
    json: async () => ({ name: 'Zainab' }),
  }));

  const name = await fetchUserName(fakeFetch);
  assert.equal(name, 'Zainab');
  assert.equal(fakeFetch.mock.calls.length, 1);
});
Enter fullscreen mode Exit fullscreen mode

You can mock functions, mock timers with mock.timers, and mock entire modules. It is not as expressive as the big mocking libraries, and if you need deep module interception or complex fake timers across a large codebase, you should still use one. But for the common case — "pretend this function returns that" — the built-in mocking is enough, and it is one less dependency to audit and update.

Coverage, watch mode, and the rest of the toolkit

First, coverage: node --test --experimental-test-coverage gives you line and branch coverage with no extra tool. It is still marked experimental, but for a small project it answers the only question that matters — which lines did my tests never touch?

Second, watch mode: node --test --watch re-runs the affected tests when you save a file. That tight loop, save and see the result a second later, is the thing that actually makes people write tests. The moment testing feels slow or heavy, people stop doing it, and a built-in watcher removes the most common excuse.

There are also reporters. The default output is human-readable. --test-reporter=tap gives you TAP output for CI, and --test-reporter=junit gives you JUnit XML if your CI system wants it. No reporter plugin to install.

When you should still install a framework

Honesty requires the other side. The built-in runner is not a replacement for everything, and pretending otherwise would be dishonest.

Install a framework when you need snapshot testing at scale, a rich plugin ecosystem, browser-based component testing, or the kind of parallelization and sharding a huge monorepo demands. If your team already knows a framework well and your CI is built around it, switching for the sake of switching is a bad trade. And if you are testing a frontend, this is not your tool at all — node:test is for Node code.

But notice what that list leaves out: most libraries, most CLIs, most small services, and most side projects. That is a lot of code currently dragging around a test framework, an assertion library, a mocking library, and a config file, for features it never uses.

The real argument is maintenance

Dependencies are not free. Every one of them is a future upgrade, a future breaking change, a future security advisory, and a future afternoon spent figuring out why the new version wants a different config format. A test setup with zero dependencies has none of those costs. It also has a property no framework can match: it will still work, unchanged, in five years, because it ships with the runtime.

Try it on your next small project. Write one test file, run node --test, and see how far you get before you miss the framework. My guess is you will get further than you expect — and the project will be lighter for it.

Top comments (0)