On this page
The per-test isolation scope: how every environment mutation is recorded and undone when the test ends, and how overlapping isolations are detected.
#typescript/src/scope
#typescript/src/scope
The per-test isolation scope: what every helper in this package mutates through, and what undoes itself when the test finishes.
This is the Node analogue of the Go module's testing.TB plumbing. Go gets TB.Setenv (record + restore + a panic under T.Parallel) and TB.Cleanup for free; Node's test runner offers only t.after, so the recording, the restoring, and the interleaving detection are implemented here.
#CleanupRegistry
export interface CleanupRegistryThe minimum a test runner's per-test context must offer for this package to bind to it: a way to register a callback that runs when the test finishes.
Node's built-in runner satisfies this directly -- a node:test TestContext has after, so isolate(t) works with no adapter. Other runners need a one-line object literal:
import { onTestFinished } from "vitest";
isolate({ after: onTestFinished });#Scope
export interface ScopeOne test's recorded mutations, in the order they were made.
#scopeFor
export function scopeFor(registry: CleanupRegistry): Scope Return the scope for registry, creating and arming it on first use.
#setEnv
export function setEnv(scope: Scope, name: string, value: string): voidSet name to value for the rest of the test, restoring whatever was there (including its absence) when the test finishes.
#unsetEnv
export function unsetEnv(scope: Scope, name: string): voidRemove name from the environment for the rest of the test, restoring whatever was there when the test finishes.
#onRelease
export function onRelease(scope: Scope, undo: () => void): void Register an arbitrary undo callback on scope.