stricttest v0.2.0 /typescript/src/scope
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

TS typescript
export interface CleanupRegistry

The 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:

TS ts
import { onTestFinished } from "vitest";
isolate({ after: onTestFinished });

#Scope

TS typescript
export interface Scope

One test's recorded mutations, in the order they were made.

#scopeFor

TS typescript
export function scopeFor(registry: CleanupRegistry): Scope

Return the scope for registry, creating and arming it on first use.

#setEnv

TS typescript
export function setEnv(scope: Scope, name: string, value: string): void

Set name to value for the rest of the test, restoring whatever was there (including its absence) when the test finishes.

#unsetEnv

TS typescript
export function unsetEnv(scope: Scope, name: string): void

Remove name from the environment for the rest of the test, restoring whatever was there when the test finishes.

#onRelease

TS typescript
export function onRelease(scope: Scope, undo: () => void): void

Register an arbitrary undo callback on scope.

Search