@contractspec/lib.testing
Golden tests ensure new rollouts behave exactly like the traffic that inspired them. Record requests in production, replay them locally, and ship with confidence.
Installation
bun add @contractspec/lib.testingRecord traffic
import {
TrafficRecorder,
InMemoryTrafficStore,
} from '@contractspec/lib.testing/recorder';
const recorder = new TrafficRecorder({
store: new InMemoryTrafficStore(),
sampleRate: 0.02,
sanitize: (snapshot) => ({
...snapshot,
input: { ...snapshot.input, secret: undefined },
}),
});
await recorder.record({
operation: { name: 'orders.create', version: 6 },
input: payload,
output,
success: true,
tenantId: ctx.organizationId ?? undefined,
});Generate suites
import { GoldenTestGenerator } from '@contractspec/lib.testing';
const generator = new GoldenTestGenerator();
const code = generator.generate(snapshots, {
suiteName: 'orders.create golden path',
runnerImport: './tests/run-operation',
runnerFunction: 'runOrdersCommand',
framework: 'vitest',
});CLI workflow
contractspec test generate \
--operation orders.create \
--output tests/orders.create.golden.test.ts \
--runner-import ./tests/run-operation \
--runner-fn runOrdersCommand \
--from-production \
--days 7 \
--sample-rate 0.05Framework agnostic
Vitest by default, Jest via `generateJestSuite`, or call `runGoldenTests` manually inside CI.
Sanitize & sample
Scrub payloads before persistence and control sample rates per operation to stay within compliance limits.