Use this file to discover all available pages before exploring further.
SnapshotAgent extends MockAgent to provide automatic record-and-replay testing. Instead of writing interceptors by hand, you run your tests once in record mode against a real server — the agent captures every request and response to a JSON snapshot file. Subsequent test runs use playback mode to replay those recorded responses without touching the network, giving you deterministic integration tests that behave exactly like a live call did.
SnapshotAgent is experimental and subject to change. Node.js will emit an ExperimentalWarning the first time it is instantiated in a process.
Controls agent behaviour. 'record' makes real requests and saves responses. 'playback' serves saved responses. 'update' replays existing snapshots and records new ones.
Loads the snapshot file and serves recorded responses without making real requests. In 'playback' mode, snapshotPath is required. A request that does not match any snapshot throws UndiciError.
Playback mode
import { SnapshotAgent, setGlobalDispatcher } from 'undici'const agent = new SnapshotAgent({ mode: 'playback', snapshotPath: './test/snapshots/api.json'})setGlobalDispatcher(agent)// Served from snapshot — no network callconst response = await fetch('https://api.example.com/users')
Uses existing snapshots when a match is found. For requests with no recorded snapshot, a real network request is made and its response is saved. snapshotPath is required.
Update mode
import { SnapshotAgent, setGlobalDispatcher } from 'undici'const agent = new SnapshotAgent({ mode: 'update', snapshotPath: './test/snapshots/api.json'})setGlobalDispatcher(agent)// Uses snapshot if exists, otherwise records a new oneconst response = await fetch('https://api.example.com/new-endpoint')await agent.saveSnapshots()
Loads snapshots from a file. In playback and update modes, this is called automatically at construction. Call it manually if you need to load from an additional file.
Multiple identical requests are recorded as separate sequential entries. In playback mode they are replayed in order; the last response repeats for any additional calls beyond what was recorded.
Environment-controlled record/playback in node:test
import { test } from 'node:test'import assert from 'node:assert/strict'import { SnapshotAgent, setGlobalDispatcher, getGlobalDispatcher } from 'undici'// SNAPSHOT_MODE=record npm test → record against the live API// npm test → replay from snapshotconst mode = process.env.SNAPSHOT_MODE ?? 'playback'test('GET /users returns an array', async (t) => { const original = getGlobalDispatcher() const agent = new SnapshotAgent({ mode, snapshotPath: './test/snapshots/users.json' }) setGlobalDispatcher(agent) t.after(async () => { if (mode === 'record') { await agent.saveSnapshots() } await agent.close() setGlobalDispatcher(original) }) const response = await fetch('https://jsonplaceholder.typicode.com/users') const users = await response.json() assert.equal(response.status, 200) assert.ok(Array.isArray(users)) assert.ok(users.length > 0)})
Use a test helper that creates a SnapshotAgent per test suite with a descriptive snapshot file name. This keeps snapshot files small and makes failures easy to diagnose.