Write a first test with node test and assert
Write and run a first test with the built-in node:test module and node:assert/strict, using node --test and no dependencies.
Node.js 24 ships a test runner, so a first test needs no install. Import test from node:test and the strict assertions from node:assert/strict.
import test from 'node:test';
import assert from 'node:assert/strict';
function add(a, b) {
return a + b;
}
test('adds two numbers', () => {
assert.equal(add(2, 3), 5);
});node --test✔ adds two numbers (0.5ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 45.2The timings will differ. node --test runs every file that matches the default patterns, such as *.test.mjs and files in a test directory, each in its own process. The assert/strict module makes equal use === and deepEqual use strict comparison, so assert.equal('5', 5) fails. See the test runner docs.
A failing assertion prints the expected and actual values with a diff, and the process exits with code 1, which is what CI looks at. Use .mjs or "type": "module" for the import syntax. The node:test module has been stable since Node.js 20.