Testing
Unit-test workflows with an in-memory engine harness, crash/flaky-step injection, and replay assertions — no Postgres, no Redis, no real time.
@dudousxd/nestjs-durable-testing runs a whole workflow in a unit test — in-memory store and
transport, a clock you control, and assertions that read back the recorded state.
pnpm add -D @dudousxd/nestjs-durable-testingA test engine
import { createTestEngine, assertRunStatus, assertOutput } from '@dudousxd/nestjs-durable-testing';
const t = createTestEngine(); // { engine, store, transport, clock, tick }
t.engine.register('checkout', '1', async (ctx) => {
await ctx.step('reserve', {});
return ctx.step('ship', {});
});
t.transport.handle('reserve', () => reserve()); // register each step's handler on the transport
t.transport.handle('ship', () => ship());
const { runId } = await t.engine.start('checkout', order, 'run1'); // enqueues → { status: 'pending' }
await t.engine.waitForRun(runId); // resolves when the run settles
await assertRunStatus(t.store, 'run1', 'completed');One call instead of start + waitForRun
t.run(workflow, input, runId, opts) is the same start then waitForRun in one shot, for tests
that only care about the settled result:
const result = await t.run('checkout', order, 'run1');
expect(result.status).toBe('completed');Control time (durable sleep)
tick(ms) advances the clock and resumes any durable sleep that is now due — no waiting:
t.engine.register('digest', '1', async (ctx) => {
await ctx.step('draft', {});
await ctx.sleep('7 days');
await ctx.step('send', {});
});
t.transport.handle('draft', () => draft());
t.transport.handle('send', () => send());
const { runId } = await t.engine.start('digest', {}, 'run1'); // enqueues → { status: 'pending' }
await t.engine.waitForRun(runId); // settles on the durable sleep → suspended
await t.tick(7 * 24 * 60 * 60 * 1000); // the sleep is due → completes
await assertRunStatus(t.store, 'run1', 'completed');t.tick is t.clock.advance(ms) followed by engine.resumeDueTimers(clock.now()) — the clock itself
(MutableClock) is a standalone piece of createTestEngine, with set(ms) (jump to an absolute
instant) alongside advance(ms) (move forward by a delta). Reach for t.clock directly, without
tick, when you need an absolute time before a run starts, or you're driving code that only takes a
bare now: () => number and isn't wired into resumeDueTimers:
import { MutableClock } from '@dudousxd/nestjs-durable-testing';
const clock = new MutableClock(1_720_000_000_000); // starts at this instant instead of the default
clock.set(1_720_100_000_000); // jump to an absolute instant
clock.advance(60_000); // ...then move forward a minuteInject crashes & retries
failOnce and failTimes are step-body factories that throw before succeeding, so a test can drive
the engine's retry/resume path deterministically instead of hoping a real dependency flakes. Each
returns a fresh step body that counts its own calls: failOnce(value) throws once then returns
value; failTimes(n, value) throws the first n calls then returns it (failOnce is exactly
failTimes(1)). Both take an optional custom Error as the last argument.
import { failOnce, failTimes, assertStepAttempts } from '@dudousxd/nestjs-durable-testing';
// Throws once, then succeeds — proves the step is retried and the run resumes on attempt 2.
t.engine.register('wf', '1', async (ctx) => ctx.step('charge', {}, { retries: 3 }));
t.transport.handle('charge', failOnce({ ok: true }));
const { runId } = await t.engine.start('wf', {}, 'run1');
await t.engine.waitForRun(runId);
await assertStepAttempts(t.store, 'run1', 'charge', 2); // failed once, then succeededUse failTimes(n, …) to exhaust a retry budget — the classic "does a poison step actually fail the
run after retries?" test — optionally with a specific error so the assertion is precise:
// retries: 2 means 3 total attempts; failing all 3 must fail the run.
t.engine.register('wf', '1', async (ctx) => ctx.step('flaky', {}, { retries: 2 }));
t.transport.handle('flaky', failTimes(3, { ok: true }, new Error('upstream 503')));
const { runId } = await t.engine.start('wf', {}, 'run1');
await t.engine.waitForRun(runId);
await assertRunStatus(t.store, 'run1', 'failed');
await assertStepAttempts(t.store, 'run1', 'flaky', 3); // all attempts thrown → run failedAssertions
assertRunStatus, assertOutput, assertStepsRan, assertStepAttempts, and recordedSteps —
all read the store, so they work against any run the engine produced.
Replay a captured run in CI
A code change can rename, reorder, or remove a step at a position an in-flight run already recorded a
checkpoint for. The engine catches that as a NonDeterminismError on replay — but you want to catch it
in CI, before it reaches a real in-flight run on deploy. assertReplayable(register, history) replays a
captured run's history against the CURRENT workflow code and rethrows if they diverged.
The loop is: capture a representative run once, commit it as a JSON fixture, and assert it on every CI run.
Capture it. From a real deployment, use the CLI, which reads your configured store:
durable export wrun_8Kb2 --out test/fixtures/pipeline.jsonOr in code, from a WorkflowEngine or a bare StateStore:
import { captureHistory, serializeRunHistory } from '@dudousxd/nestjs-durable-testing';
import { writeFile } from 'node:fs/promises';
const history = await captureHistory(engine, 'wrun_8Kb2'); // null for an unknown run
if (history) await writeFile('test/fixtures/pipeline.json', serializeRunHistory(history));Assert it. parseRunHistory revives the Date fields JSON flattened to strings:
import { readFileSync } from 'node:fs';
import type { WorkflowEngine } from '@dudousxd/nestjs-durable-core';
import { assertReplayable, parseRunHistory } from '@dudousxd/nestjs-durable-testing';
const registerPipeline = (engine: WorkflowEngine) =>
engine.register('pipeline', '1', pipeline.run); // register exactly as the app does
it('replays the recorded fixture against the current workflow code', async () => {
const fixture = parseRunHistory(readFileSync('test/fixtures/pipeline.json', 'utf8'));
await assertReplayable(registerPipeline, fixture); // throws NonDeterminismError on divergence
});| Helper | What it does |
|---|---|
captureHistory(source, runId) | { run, checkpoints } from anything with getRun + listCheckpoints (an engine or a store); null for an unknown run |
serializeRunHistory(history) | pretty JSON with a format marker (version: 1) and a trailing newline, the same format durable export writes |
parseRunHistory(json) | back to a RunHistory with real Dates; throws on JSON that isn't a fixture, or on a fixture whose version isn't RUN_HISTORY_FIXTURE_VERSION (a fixture with no version is read as version 1) |
assertReplayable(register, history) | replays the history against the current code; throws NonDeterminismError on divergence |
A fixture holds the run's payloads (input, outputs, step logs, errors), decoded if your store is
wrapped in a CodecStateStore. Pick a run whose data you can commit, or scrub it first.
Testing a custom store/transport/admission backend
If you're writing a new StateStore, Transport, or AdmissionBackend adapter, don't write your own
behavioral spec from scratch — run the shared conformance suite the shipped adapters already run
against. Each one throws (or fails its it block) on any divergence from the canonical (in-memory)
semantics.
State store — runStateStoreContract(name, makeStore) takes a StateStoreFactory that returns a
fresh, empty store plus a cleanup:
import { runStateStoreContract } from '@dudousxd/nestjs-durable-testing';
import { MyStateStore } from './my-state-store';
runStateStoreContract('MyStateStore', async () => ({
store: new MyStateStore(/* fresh, empty */),
cleanup: async () => {
/* close the connection, drop the schema, etc. */
},
}));If the factory can't reach its backend at all (no Docker, no local Postgres), throw
StateStoreUnavailableError from it instead of letting the connection error fail the suite — the
contract skips that case gracefully rather than reporting a false failure.
Return a statements: () => number from the factory too (a counter fed by your driver's query log)
and the contract also checks that saveCheckpoints, getCheckpoints and saveCheckpointsIfPending
send a few statements for a 600-row batch, not one per row. Return a statementLog: () => string[]
(each statement's SQL text followed by its parameters, from the same log) and it checks that
settleCheckpointsIfPending sends none of the input of the steps it settles, and that
getCheckpointsForSettle and listCheckpointsForReplay select neither the input column nor * (the replay read, for a run whose steps completed).
Transport — assertTransportConformance(transport, idPrefix?) proves a durable remote step
round-trips to a worker and back, for both success and a throwing handler:
import { assertTransportConformance } from '@dudousxd/nestjs-durable-testing';
import { MyTransport } from './my-transport';
it('conforms to the Transport contract', async () => {
await assertTransportConformance(new MyTransport());
});Step fan results — runStepFanResultsContract(name, makeHarness, options?) runs ctx.all and
ctx.allSettled step fans, with and without concurrency, whose results back up on your broker,
over two engine instances. It checks that every step settles once, the parent turns once per wave,
the results are settled in batches (the consumer hands the engine many at once), and the fan's steps
go out through dispatchMany (pass bulkDispatch: false for a transport without one). The harness
opens fresh queues per case and returns two engine-side transports plus a worker serving
STEP_FAN_STEP.
Admission backend — runAdmissionBackendContract(name, makeBackend) takes an
AdmissionBackendFactory ((clock) => AdmissionBackend) and asserts concurrency caps, rate limits,
priority, FIFO/LIFO ordering, and fairness:
import { runAdmissionBackendContract } from '@dudousxd/nestjs-durable-testing';
import { MyAdmissionBackend } from './my-admission-backend';
runAdmissionBackendContract('MyAdmissionBackend', (clock) => new MyAdmissionBackend(clock));If your store is a new ORM adapter, also assert its physical column names against the canonical
snake_case contract, so a mismatch fails a unit test instead of "Unknown column" at runtime:
assertDurableColumns(resolve) walks every table/property in DURABLE_CANONICAL_COLUMNS and returns
the mismatches (empty when conformant) — expect(assertDurableColumns(resolve)).toEqual([]). If your
adapter intentionally preserves camelCase columns instead of following the snake_case contract, build
the matching expectation map with preserveColumnExpectation() and pass it in place of the default.
Typed dashboard client
Generate a fully-typed client (and TanStack hooks) for the durable dashboard's REST API — list/inspect runs, retry, cancel, deliver webhooks — with the nestjsDurableCodegen extension for @dudousxd/nestjs-codegen.
CLI
Inspect, cancel and export workflow runs from the terminal — `durable inspect` (including the `--stale` stranded-run view), `durable cancel`, `durable export` for CI replay fixtures, and `durable lifecycle:republish`.