The TypeScript SDK ships a dedicatedDocumentation Index
Fetch the complete documentation index at: https://docs.trulayer.ai/llms.txt
Use this file to discover all available pages before exploring further.
@trulayer/sdk/testing subpath with an in-memory sender and a fluent assertion chain. No API key is required, no network calls are made, and the helpers work under Vitest, Jest, Mocha, or any runner that treats thrown errors as failures.
Install
Write your first test
API
createTestClient(overrides?)
Returns { client, sender }. The client is a fully functional TruLayer wired to an in-memory LocalBatchSender. Pass partial config overrides (sampling rate, redaction callback, project name) to exercise specific client behavior:
assertSender(sender)
Entry point for the fluent assertion chain. Each method returns this (or, for hasTrace, a TraceAssertions scoped to a specific trace) so assertions chain naturally.
| Method | Scope | Behaviour |
|---|---|---|
.hasTrace(traceId?) | sender | Asserts the sender captured at least one trace, or the trace with the given ID. Returns a TraceAssertions. |
.spanCount(n) | sender | Asserts the total span count across all captured traces. |
.hasSpanNamed(name) | sender | Asserts at least one span with the given name is present across all traces. |
.spanCount(n) | trace | Asserts the per-trace span count. |
.hasSpanNamed(name) | trace | Asserts the trace contains a span with the given name; the error message lists observed span names. |
.hasAttribute(key, value) | trace | Asserts at least one span carries the given attribute. |
hasAttribute lookup order
hasAttribute(key, value) first looks up the key on each span’s metadata object (where span.setMetadata({...}) writes and where auto-instrumenters following the OpenTelemetry GenAI conventions place their attributes). If not found, it falls back to a short list of well-known top-level fields:
modelnamespan_typeprompt_tokenscompletion_tokens
model: "gpt-4o" without knowing whether the instrumenter wrote the value to span.data.model or to metadata["model"].
Comparison is deep-equal, so objects and arrays work too:
Replay captured traces
LocalBatchSender.flushToFile(path) serializes every captured trace to a JSONL file — one JSON object per line. replay({ file }) reads the file and re-emits each trace through a new (or caller-provided) LocalBatchSender, which makes golden-file regression tests and reproducing production traces locally straightforward.
console.warn — the helper follows the SDK’s never-throws contract so a single corrupt line in a fixture never takes down an entire test run.
Running captures via environment variables
For integration tests that spin up a full server, set the replay mode variables on the process before loading your app.init() wires them up automatically:
TRULAYER_MODE=replay also forces local mode — replayed traces never escape to the live API, because they were produced by a previous capture and would double-count.
See also
- Failure behavior — how the SDK handles ingest outages and how to opt in to block mode.
- TypeScript SDK reference — full type signatures.