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.
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.
function registerContextConformance(
name: string,
factory: ContextFactory,
runner: ContextConformanceRunner,
): voidParameters
| Name | Type | Description |
|---|---|---|
| name | string | Name of the suite, such as the context's name. |
| factory | ContextFactory | Builds the context and a fresh working folder for each case, and says what it promises beyond the base contract. |
| runner | ContextConformanceRunner | Your test runner's |
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 })registerPortConformanceFunction#
Register only the port cases (ExecutionContext.ports) under name:
for a context whose ports is new, or proven apart from the rest.
function registerPortConformance(
name: string,
factory: ContextFactory,
runner: ContextConformanceRunner,
): voidParameters
| Name | Type | Description |
|---|---|---|
| name | string | Name of the suite. |
| factory | ContextFactory | Builds the context and a fresh working folder for each case. |
| runner | ContextConformanceRunner | Your test runner's |
Interfaces#
ContextConformanceRunnerInterface#
The test functions the suite needs.
interface ContextConformanceRunnerProperties
| Name | Type | Description |
|---|---|---|
| describe | (name: string, body: () => void) => void | Your runner's |
| it | (name: string, body: () => Promise<void>) => void | Your runner's |
| 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. |
ContextFactoryInterface#
How to build the context under test, and what it promises beyond the base contract.
interface ContextFactoryProperties
| Name | Type | Description |
|---|---|---|
| confined? | boolean | The context refuses file paths that resolve outside the handle's working
folder ( |
| 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 |
| workspace | () => Promise<ContextWorkspace> | Prepares a fresh working folder for one case. |
ContextWorkspaceInterface#
A fresh, empty working folder for one case.
interface ContextWorkspaceProperties
| Name | Type | Description |
|---|---|---|
| dispose? | () => Promise<void> | Removes what |
| spawn | SpawnConfig | What the case spawns its handles with. Its |