Skip to content

Back to Releasing JavaScript

Testing your code

With Node.js 20 reaching LTS (long-term support) stage, we can start using the node:assert and node:test native modules to test JavaScript code.

Note: Because node --test only accepts a quoted glob pattern as the argument starting with Node 21, that’s the minimum version we’ll want to use, since we always want to quote our globs in scripts.

Running tests is done with node --test '<pattern>'.

The basic structure of a test file:

import test from 'node:test';
import assert from 'node:assert';

test('my test', t => {
assert.equal(1, 2);
});

If the test reporter output is too raw, you can pipe it into a tool that can summarize the TAP format, such as faucet.

A piece of trivia. The default spec test reporter will show how the expected and actual objects compared in a deepEqual or deepStrictEqual assertion differ. However, the diff is suppressed when the assertion contains a custom message (the third argument). See nodejs/node#52596.