Test runner

test() from node:test

Registers a test. A synchronous throw or rejected returned promise fails the test. The native runner reports failures and sets a nonzero exit code; the fixture launches a deliberately failing test in a disposable child. Tested on Node.js 22.16.0. Example output (one line per entry): ["1 true"].

Formal Syntax & Structure

nodejs syntaxnodejs
test([name][, options][, fn])

Parameters & Attributes

NameTypeDescription
namestringTest name.
optionsobjectRunner options.
fnFunctionTest body.

Return Value

Promise<void>

Completion of test registration/execution.

Working Example

Example Snippetnodejs
import { spawnSync } from 'node:child_process';import { writeFileSync, mkdtempSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';const dir = mkdtempSync(join(tmpdir(), 'bm-test-'));try {    writeFileSync(join(dir, 'case.test.mjs'), "import test from 'node:test';import assert from 'node:assert/strict';test('inventory',()=>assert.equal(1,2));");    const r = spawnSync(process.execPath, ['--test', 'case.test.mjs'], { encoding: 'utf8', timeout: 3000, cwd: dir });    if (r.error)        throw r.error;    console.log(r.status, /not ok 1 - inventory/.test(r.stdout));}finally {    rmSync(dir, { recursive: true, force: true });}
Node.js Reference