---
title: "Node.js tests without a library: node:test and node --test"
description: "Node.js ships its own test runner, the node:test module, stable since v20.0.0. You write test(), describe() and it() as usual, and run every test file with one command: node --test."
url: https://ihsanalapsi.dev/articles/nodejs-test-runner-node-test
lang: en
date: 2026-11-26
updated: 2026-11-26
tags: ["Developer tools", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Node.js tests without a library: node:test and node --test

Every new Node.js project used to start with the same ritual: install a test library, its configuration, and a few libraries alongside it. Today the tool itself ships inside Node.js. By the end of this article you will write your first test file with the `node:test` module, run every test in your project with one command, and know when it is enough and when it isn't.

## What is node:test?

The `node:test` module is a test runner built into Node.js that lets you write and run tests without any library from npm.

According to [the Node.js test runner documentation](https://nodejs.org/api/test.html), the module was added in v18.0.0, became stable in v20.0.0, and its stability level in the documentation today is "Stable". The documentation also says it is only available under the `node:` scheme, so `require('test')` won't work.

## How do you write your first test?

You import `test` from `node:test` and `assert` from `node:assert`, and write each case as a function. The test passes if the function doesn't throw, and fails if it does.

```js
// sum.test.js
import test from 'node:test';
import assert from 'node:assert';

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

test('sum adds two numbers', () => {
  assert.strictEqual(sum(2, 3), 5);
});

test('async test waits for the promise', async () => {
  const value = await Promise.resolve(sum(1, 1));
  assert.strictEqual(value, 2);
});
```

The same rule applies to async functions: the documentation explains that the test fails when the Promise returned by the function is rejected. So you don't need anything extra, just `await`.

If you are used to the `describe` and `it` style, they are there: the documentation says `describe()` is an alias for `suite()`, and `it()` is an alias for `test()`.

## How do you run every test in the project?

You run them with one command from the project root: `node --test`. It finds the test files by their names on its own and runs them all.

```bash
node --test
```

The documentation lists the patterns it matches by default, including:


| Name pattern                | Example        |
| --------------------------- | -------------- |
| `**/*.test.{cjs,mjs,js}`    | `sum.test.js`  |
| `**/*-test.{cjs,mjs,js}`    | `sum-test.mjs` |
| `**/*_test.{cjs,mjs,js}`    | `sum_test.cjs` |
| `**/test/**/*.{cjs,mjs,js}` | `test/sum.js`  |


The same patterns also match `ts`, `mts` and `cts` files, unless you pass `--no-strip-types`. So if your files follow one of these names, you need no configuration at all.

In `package.json`, the test script becomes:

```json
{
  "scripts": {
    "test": "node --test"
  }
}
```

## What about mocks and watch mode?

Mocking lives in the same module: the documentation says `node:test` supports mocking through a top-level `mock` object, and gives an example of a spy on a function that adds two numbers, so you can check it was called the way you expect.

Watch mode reruns the affected tests every time you change a file:

```bash
node --test --watch
```

Watch out, though: the documentation still marks this mode as experimental. Use it while you write code, not in your deploy pipeline.

## When is node:test enough, and when isn't it?

`node:test` is enough for unit and API tests in a Node.js project: functions, routes and business logic. In my view it is the best starting point for a new project, because every library you don't install is a library that can't run scripts on your machine, which I covered in [the npm ignore-scripts article](/articles/npm-ignore-scripts).

But if your project is a React front end that needs a simulated browser, or you have thousands of tests on another tool that works well, there is no reason to switch today.

## Where do you start now?

Open a small Node.js project, create a `sum.test.js` file like the example above, and run `node --test`. And if a test fails one day and you don't know which commit broke it, [git bisect](/articles/git-bisect-find-bad-commit) works with the same command to find it for you. If you are building your path as a developer from the start, [the 2026 programmer roadmap](/products/roadmap-2026) helps you order the steps. Which function will you test first?

## FAQ

**Can I write require('test') instead of node:test?**

No. The Node.js documentation says this module is only available under the node: scheme, so always write node:test.

**Do TypeScript tests run with node --test?**

Yes. The documentation says node --test also matches ts, mts and cts files with the same names, unless you pass --no-strip-types.

**Is there a mode that reruns tests when I edit a file?**

Yes, node --test --watch. The documentation still marks it experimental, so use it while you work, not in your deploy pipeline.
