| 1 |
--- |
| 2 |
name: manten |
| 3 |
description: Manten testing library patterns - standalone imports via AsyncLocalStorage, async tests, describe groups, hooks, snapshots, timeouts, retries, and concurrency. Use when writing tests with manten, running node test files, or setting up a no-config test suite. |
| 4 |
--- |
| 5 |
|
| 6 |
## Quick Reference |
| 7 |
|
| 8 |
| API | Purpose | |
| 9 |
|-----|---------| |
| 10 |
| `test(name, fn, opts?)` | Run a single test. Returns `Promise<void>` | |
| 11 |
| `describe(name, fn, opts?)` | Group tests. Returns `Promise<void>` | |
| 12 |
| `expect(value)` | Jest assertions | |
| 13 |
| `expectSnapshot(value, name?)` | Snapshot testing | |
| 14 |
| `onTestFail(callback)` | Debug hook (inside `test()`) | |
| 15 |
| `onTestFinish(callback)` | Cleanup hook (inside `test()`) | |
| 16 |
| `onFinish(callback)` | Cleanup hook (inside `describe()`) | |
| 17 |
| `skip(reason?)` | Skip current test or describe group | |
| 18 |
| `setProcessTimeout(ms)` | Global process timeout | |
| 19 |
| `configure({ snapshotPath? })` | Snapshot configuration | |
| 20 |
|
| 21 |
**All APIs are standalone imports:** |
| 22 |
|
| 23 |
```ts |
| 24 |
import { |
| 25 |
test, describe, expect, skip, onTestFail, onTestFinish, onFinish, expectSnapshot |
| 26 |
} from 'manten' |
| 27 |
``` |
| 28 |
|
| 29 |
Each function automatically knows which test or group it belongs to via AsyncLocalStorage. |
| 30 |
|
| 31 |
## Core Pattern: Await Controls Flow |
| 32 |
|
| 33 |
```ts |
| 34 |
import { test, expect } from 'manten' |
| 35 |
|
| 36 |
// Concurrent (recommended default) |
| 37 |
test('A', async () => { /* ... */ }) |
| 38 |
test('B', async () => { /* runs simultaneously with A */ }) |
| 39 |
|
| 40 |
// Sequential: only await when you need ordering |
| 41 |
await test('first', async () => { /* ... */ }) |
| 42 |
await test('second', async () => { /* runs after first */ }) |
| 43 |
``` |
| 44 |
|
| 45 |
Node.js won't exit while promises are settling, so you don't need `await` to keep the process alive — only to enforce ordering. |
| 46 |
|
| 47 |
## Describe Blocks |
| 48 |
|
| 49 |
```ts |
| 50 |
import { describe, test } from 'manten' |
| 51 |
|
| 52 |
describe('Auth', () => { |
| 53 |
test('login', async () => { /* ... */ }) |
| 54 |
test('logout', async () => { /* ... */ }) |
| 55 |
}) |
| 56 |
``` |
| 57 |
|
| 58 |
Nesting: |
| 59 |
|
| 60 |
```ts |
| 61 |
describe('Outer', () => { |
| 62 |
test('A', () => { /* ... */ }) |
| 63 |
|
| 64 |
describe('Inner', () => { |
| 65 |
test('B', () => { /* ... */ }) |
| 66 |
}) |
| 67 |
}) |
| 68 |
``` |
| 69 |
|
| 70 |
Awaiting a group waits for all children: |
| 71 |
|
| 72 |
```ts |
| 73 |
await describe('Group', () => { |
| 74 |
test('A', async () => { /* ... */ }) |
| 75 |
test('B', async () => { /* ... */ }) |
| 76 |
}) |
| 77 |
// Both A and B complete before continuing |
| 78 |
``` |
| 79 |
|
| 80 |
## Timeouts & Retries |
| 81 |
|
| 82 |
`test()` and `describe()` callbacks always receive `{ signal }` — an `AbortSignal` that aborts on timeout or when the parent group is aborted: |
| 83 |
|
| 84 |
```ts |
| 85 |
// Timeout (3rd arg as number) |
| 86 |
test('fast test', async ({ signal }) => { |
| 87 |
await fetch('/api', { signal }) |
| 88 |
}, 1000) |
| 89 |
|
| 90 |
// Options object |
| 91 |
test('flaky test', async () => { |
| 92 |
await unreliableAPI() |
| 93 |
}, { |
| 94 |
timeout: 5000, |
| 95 |
retry: 3 |
| 96 |
}) |
| 97 |
``` |
| 98 |
|
| 99 |
## Hooks |
| 100 |
|
| 101 |
```ts |
| 102 |
import { test, onTestFail, onTestFinish } from 'manten' |
| 103 |
|
| 104 |
test('with cleanup', async () => { |
| 105 |
const resource = await createResource() |
| 106 |
onTestFinish(() => resource.cleanup()) // Always runs |
| 107 |
onTestFail(error => console.log('Debug:', error)) |
| 108 |
}) |
| 109 |
``` |
| 110 |
|
| 111 |
`onFinish` runs after all tests in a `describe()`: |
| 112 |
|
| 113 |
```ts |
| 114 |
import { describe, test, onFinish } from 'manten' |
| 115 |
|
| 116 |
describe('Database', async () => { |
| 117 |
const database = await connect() |
| 118 |
onFinish(() => database.close()) |
| 119 |
|
| 120 |
test('query', () => { /* ... */ }) |
| 121 |
}) |
| 122 |
``` |
| 123 |
|
| 124 |
Hook errors are logged but don't fail the test. `onFinish` errors set `process.exitCode = 1`. |
| 125 |
|
| 126 |
## Skipping |
| 127 |
|
| 128 |
```ts |
| 129 |
import { test, skip } from 'manten' |
| 130 |
|
| 131 |
test('linux only', () => { |
| 132 |
if (process.platform !== 'linux') { |
| 133 |
skip('Only runs on Linux') |
| 134 |
} |
| 135 |
// ... |
| 136 |
}) |
| 137 |
``` |
| 138 |
|
| 139 |
Skip entire groups — `skip()` must be called before any `test()` or nested `describe()`: |
| 140 |
|
| 141 |
```ts |
| 142 |
describe('GPU tests', () => { |
| 143 |
if (!hasGPU) { |
| 144 |
skip('GPU not available') |
| 145 |
} |
| 146 |
test('render', () => { /* ... */ }) // All skipped |
| 147 |
}) |
| 148 |
``` |
| 149 |
|
| 150 |
## Concurrency Control |
| 151 |
|
| 152 |
```ts |
| 153 |
describe('Database tests', () => { |
| 154 |
test('query 1', async () => { /* ... */ }) |
| 155 |
test('query 2', async () => { /* ... */ }) |
| 156 |
test('query 3', async () => { /* ... */ }) |
| 157 |
}, { parallel: 2 }) // Max 2 concurrent |
| 158 |
``` |
| 159 |
|
| 160 |
Options: `false` (sequential), `true` (unbounded), `number` (limit), `'auto'` (adapts to CPU load). |
| 161 |
|
| 162 |
Tests that you explicitly `await` bypass the parallel queue. |
| 163 |
|
| 164 |
## Group Timeouts |
| 165 |
|
| 166 |
```ts |
| 167 |
describe('Suite', () => { |
| 168 |
test('A', async () => { /* ... */ }) |
| 169 |
test('B', async () => { /* ... */ }) |
| 170 |
}, { timeout: 10_000 }) |
| 171 |
``` |
| 172 |
|
| 173 |
Individual test timeouts still apply — whichever is stricter wins. |
| 174 |
|
| 175 |
## Snapshot Testing |
| 176 |
|
| 177 |
```ts |
| 178 |
import { test, expectSnapshot } from 'manten' |
| 179 |
|
| 180 |
test('user data', () => { |
| 181 |
// Named (recommended) — order-independent |
| 182 |
expectSnapshot(getUser(), 'initial user') |
| 183 |
|
| 184 |
// Auto-numbered — keys become "user data 1", "user data 2", etc. |
| 185 |
expectSnapshot(getUser()) |
| 186 |
}) |
| 187 |
``` |
| 188 |
|
| 189 |
Snapshots are stored in `.manten.snap` by default. Serialized with `util.inspect` — output may differ across Node.js versions, so regenerate after upgrading Node. |
| 190 |
|
| 191 |
Update snapshots: `MANTEN_UPDATE_SNAPSHOTS=1 node tests/index.ts` |
| 192 |
|
| 193 |
Configure path via `configure()` or `MANTEN_SNAPSHOT_PATH` env var: |
| 194 |
|
| 195 |
```ts |
| 196 |
import { configure } from 'manten' |
| 197 |
|
| 198 |
configure({ snapshotPath: 'custom.snap' }) |
| 199 |
``` |
| 200 |
|
| 201 |
## Splitting Tests Across Files |
| 202 |
|
| 203 |
Import files inside `describe()` — they automatically nest under the parent group: |
| 204 |
|
| 205 |
```ts |
| 206 |
// tests/index.ts |
| 207 |
import { describe } from 'manten' |
| 208 |
|
| 209 |
describe('my-app', async () => { |
| 210 |
import('./specs/auth.ts') |
| 211 |
import('./specs/api.ts') |
| 212 |
|
| 213 |
// Or add `await` to run files sequentially |
| 214 |
}) |
| 215 |
``` |
| 216 |
|
| 217 |
```ts |
| 218 |
// tests/specs/auth.ts |
| 219 |
import { describe, test, expect } from 'manten' |
| 220 |
|
| 221 |
describe('Authentication', () => { |
| 222 |
test('login', () => { /* ... */ }) |
| 223 |
test('logout', () => { /* ... */ }) |
| 224 |
}) |
| 225 |
``` |
| 226 |
|
| 227 |
Each file works standalone too — `node tests/specs/auth.ts` runs just that file. |
| 228 |
|
| 229 |
### Parameterized Test Files |
| 230 |
|
| 231 |
Export a function wrapping `describe()` to pass data into test files: |
| 232 |
|
| 233 |
```ts |
| 234 |
// tests/specs/builds.ts |
| 235 |
import { describe, test, expect } from 'manten' |
| 236 |
|
| 237 |
export const builds = (nodePath: string) => describe('builds', () => { |
| 238 |
test('compiles', async () => { |
| 239 |
const result = await run(nodePath) |
| 240 |
expect(result.exitCode).toBe(0) |
| 241 |
}) |
| 242 |
}) |
| 243 |
``` |
| 244 |
|
| 245 |
Since `describe()` doesn't run until called, these can be statically imported: |
| 246 |
|
| 247 |
```ts |
| 248 |
// tests/index.ts |
| 249 |
import { builds } from './specs/builds.ts' |
| 250 |
import { describe } from 'manten' |
| 251 |
|
| 252 |
describe('my-app', async () => { |
| 253 |
for (const nodeVersion of ['v20', 'v22', 'v24']) { |
| 254 |
const node = await getNode(nodeVersion) |
| 255 |
await describe(`Node ${node.version}`, () => { |
| 256 |
builds(node.path) |
| 257 |
}) |
| 258 |
} |
| 259 |
}) |
| 260 |
``` |
| 261 |
|
| 262 |
Export names should match the file name or `describe()` description. |
| 263 |
|
| 264 |
## Recommended Project Structure |
| 265 |
|
| 266 |
``` |
| 267 |
tests/ |
| 268 |
specs/ # test files |
| 269 |
utils/ # shared test helpers |
| 270 |
fixtures/ # static test data |
| 271 |
index.ts # entry point — run this |
| 272 |
``` |
| 273 |
|
| 274 |
## Running Tests |
| 275 |
|
| 276 |
```sh |
| 277 |
# Run tests (Node 22.6+ runs TypeScript natively) |
| 278 |
node tests/index.ts |
| 279 |
|
| 280 |
# Watch mode |
| 281 |
node --watch tests/index.ts |
| 282 |
|
| 283 |
# Run specific test by substring match (case-sensitive) |
| 284 |
TESTONLY='login' node tests/index.ts |
| 285 |
|
| 286 |
# Update snapshots |
| 287 |
MANTEN_UPDATE_SNAPSHOTS=1 node tests/index.ts |
| 288 |
``` |
| 289 |
|
| 290 |
## Process-Level Timeout |
| 291 |
|
| 292 |
```ts |
| 293 |
import { setProcessTimeout } from 'manten' |
| 294 |
|
| 295 |
setProcessTimeout(10 * 60 * 1000) // 10 minutes max |
| 296 |
``` |
| 297 |
|
| 298 |
## Key Behaviors |
| 299 |
|
| 300 |
- **Tests run immediately** — no collection phase. `test()` executes as soon as it's called. |
| 301 |
- **No beforeAll/beforeEach** — inline setup in each test, or use `describe()` + `onFinish()` for shared resources. |
| 302 |
- **Exit code** — failures set `process.exitCode = 1` but don't force-exit. All tests run to completion. |
| 303 |
- **`TESTONLY`** — matches against full title including describe prefixes (e.g. `Auth › login`). Case-sensitive. |
| 304 |
- **`signal`** — always provided to `test()` and `describe()` callbacks, not just when timeouts are set. |
| 305 |
- **`skip()` in describe** — must be called before any `test()` or nested `describe()`. |
| 306 |
- **TypeScript** — all APIs fully typed. `Test` and `Describe` types exported for advanced use cases. |
| 307 |
|
| 308 |
## Migrating from v1 |
| 309 |
|
| 310 |
For migrating from manten v1 (callback destructuring) to v2 (standalone imports), see [MIGRATION.md](./MIGRATION.md). |
| 311 |
|
| 312 |
## Related |
| 313 |
|
| 314 |
For disposable file system fixtures, use [`fs-fixture`](https://github.com/privatenumber/fs-fixture) with `await using` for automatic cleanup. |