Test runner
t.after()
Registers cleanup after the current test, including when the test fails. Use it to close resources owned by the test. It is not a substitute for cleanup after a process is killed. Tested on Node.js 22.16.0. Example output (one line per entry): ["1 true"].
Formal Syntax & Structure
nodejs syntaxnodejs
t.after(fn[, options])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fn | Function | Cleanup hook. |
| options | object | Hook options. |
Return Value
undefined
Registers cleanup.
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('owner',t=>{t.after(()=>console.log('cleaned-fixture'));assert.fail('intentional');});"); 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, r.stdout.includes('cleaned-fixture'));}finally { rmSync(dir, { recursive: true, force: true });}