Skip to content

zidane/contexts/testing

The conformance kit for execution contexts, with cases for files, commands, aborts, streams, background tasks and host restarts.

import { … } from 'zidane/contexts/testing'src/contexts/testing/index.ts

Use registerContextConformance in the tests of an execution context of your own, or to prove a built-in one on your machine. It registers the cases of the context contract with your test runner. The cases cover files, commands with their aborts and timeouts, streams, background tasks, and what survives a host restart. create builds the context, and workspace gives each case a fresh, empty working folder. confined adds the cases that refuse paths outside that folder, and reattaches adds the restart cases. The kit does not register a case that the context cannot take part in. See execution contexts.

TypeScript
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, it, setDefaultTimeout } from 'bun:test'
import { createProcessContext } from 'zidane/contexts'
import { registerContextConformance } from 'zidane/contexts/testing'

// A case waits up to 25 s for a condition: give it a budget above that.
setDefaultTimeout(60_000)

registerContextConformance('process', {
  create: () => createProcessContext(),
  workspace: async () => {
    const cwd = await mkdtemp(join(tmpdir(), 'conformance-'))
    return { spawn: { cwd }, dispose: () => rm(cwd, { recursive: true, force: true }) }
  },
  reattaches: true,
}, { describe, it })

Functions#

registerContextConformanceFunction#

Register the suite under name.

TypeScript
function registerContextConformance(
  name: string,
  factory: ContextFactory,
  runner: ContextConformanceRunner,
): void
Parameters
NameTypeDescription
namestring

Name of the suite, such as the context's name.

factoryContextFactory

Builds the context and a fresh working folder for each case, and says what it promises beyond the base contract.

runnerContextConformanceRunner

Your test runner's describe and it, and how long a case waits for a condition.

Example
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, it, setDefaultTimeout } from 'bun:test'
import { createProcessContext } from 'zidane/contexts'
import { registerContextConformance } from 'zidane/contexts/testing'

setDefaultTimeout(60_000)
registerContextConformance('process', {
  create: () => createProcessContext(),
  workspace: async () => {
    const cwd = await mkdtemp(join(tmpdir(), 'conformance-'))
    return { spawn: { cwd }, dispose: () => rm(cwd, { recursive: true, force: true }) }
  },
  reattaches: true,
}, { describe, it })

src/contexts/testing/conformance.ts:61

registerPortConformanceFunction#

Register only the port cases (ExecutionContext.ports) under name: for a context whose ports is new, or proven apart from the rest.

TypeScript
function registerPortConformance(
  name: string,
  factory: ContextFactory,
  runner: ContextConformanceRunner,
): void
Parameters
NameTypeDescription
namestring

Name of the suite.

factoryContextFactory

Builds the context and a fresh working folder for each case.

runnerContextConformanceRunner

Your test runner's describe and it, and how long a case waits for a condition.

src/contexts/testing/conformance.ts:28

Interfaces#

ContextConformanceRunnerInterface#

The test functions the suite needs.

TypeScript
interface ContextConformanceRunner
ContextConformanceRunner properties
NameTypeDescription
describe(name: string, body: () => void) => void

Your runner's describe: groups cases under name.

it(name: string, body: () => Promise<void>) => void

Your runner's it: registers one async case.

waitMs?number

How long a case waits for a condition (a process to die, output to arrive) before it gives up, in milliseconds. Default 25 000. Give each case a test budget above it.

src/contexts/testing/harness.ts:45

ContextFactoryInterface#

How to build the context under test, and what it promises beyond the base contract.

TypeScript
interface ContextFactory
ContextFactory properties
NameTypeDescription
confined?boolean

The context refuses file paths that resolve outside the handle's working folder (.., absolute paths, symlinks pointing out), such as a process context with workspaceRoot. The cases write a probe file next to the working folder through the shell, and remove it.

create() => ExecutionContext

Builds an instance of the context. Called once at registration (the instance every case runs on) and once more per restart case: a second instance on the same environment, as a restarted host builds it.

reattaches?boolean

A new instance from create can operate a handle an earlier instance spawned (after its attach, when it has one): the process context, sandbox providers. Absent: the restart cases are skipped.

workspace() => Promise<ContextWorkspace>

Prepares a fresh working folder for one case.

src/contexts/testing/harness.ts:20

ContextWorkspaceInterface#

A fresh, empty working folder for one case.

TypeScript
interface ContextWorkspace
ContextWorkspace properties
NameTypeDescription
dispose?() => Promise<void>

Removes what ContextFactory.workspace made, after the case destroyed its handles.

spawnSpawnConfig

What the case spawns its handles with. Its cwd must be an empty folder (or one spawn creates).

src/contexts/testing/harness.ts:12