Test runner
t.test()
Creates a subtest under a parent test. Await it so the parent stays alive until the subtest finishes; otherwise the parent can finish first and cancel outstanding work. Tested on Node.js 22.16.0. Example output (one line per entry): ["0 true"].
Formal Syntax & Structure
nodejs syntaxnodejs
t.test([name][, options][, fn])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Subtest name. |
| options | object | Test options. |
| fn | Function | Subtest body. |
Return Value
Promise<void>
Subtest completion.
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('parent',async t=>{await t.test('child',()=>assert.equal(2+2,4));});"); 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, /ok 1 - child/.test(r.stdout));}finally { rmSync(dir, { recursive: true, force: true });}