Class RecordingReporter

A reporter for tests: it keeps every event it receives, in order, and the latest value of each entry of global metadata, for a test to read once the journey is over. It never fails.

A test adds one to a workflow instance, runs the journey, then reads its events, its globalMetadata and its path.

import assert from "node:assert/strict";
import { LocalExecutor } from "itinera/executor";
import { Ending, RecordingReporter, stepMock } from "itinera/testing";
import { Workflow } from "itinera/workflow";

class Orders extends Workflow {}

export default (async () => {
const charge = stepMock({
attempts: [Ending.abnormalTermination("timeout"), Ending.success],
});
const orders = Orders.builder()
.name("orders")
.step(charge.builder("charge").retryBudget(1))
.build();
const recorder = new RecordingReporter();
const instance = orders.instance(
{ order: "A-1" },
{ reporters: [recorder], reportersGlobalMetadata: { tenant: "acme" } },
);

await new LocalExecutor().run(instance);

assert.deepEqual(
recorder.events.map((event) => event.kind),
[
"journey_started",
"attempt_started",
"step_abnormal_termination",
"step_retrying",
"attempt_started",
"step_succeeded",
"journey_succeeded",
],
);
assert.deepEqual(recorder.path, [
"charge attempt 1 terminated abnormally: timeout",
"charge retrying",
"charge attempt 2 succeeded",
"journey succeeded",
]);
assert.deepEqual(
[...recorder.globalMetadata],
[
["journey_id", instance.journeyId],
["tenant", "acme"],
],
);
})();

Hierarchy (View Summary)

Implements

Index

  • get globalMetadata(): ReadonlyMap<string, PlainValue>

    The global metadata it has received so far: each key with its latest value, in the order the keys were first set. The journey ID is the first entry, under journey_id.

    Returns ReadonlyMap<string, PlainValue>

  • get path(): readonly string[]

    The path of the journey so far: one line for each event of its flow, in the order it received them, so that a test can compare it with the path it expects, or with an approved file through path.join("\n").

    Only the flow has lines: the outcome of each attempt, each decision on a step, and how the journey ended. Inputs, services, hooks, contributions, the events steps, hooks and service providers emit, and global metadata have none, and no line holds a timestamp, a sequence number or an ID, so a path changes only when the flow does. The lines read:

    • charge attempt 1 succeeded; charge attempt 1 failed: declined, with the reason's code; charge attempt 1 skipped, with : and the reason's code when it has one; and charge attempt 1 terminated abnormally: timeout, with the error's message. A rescue step's attempts read the same under its own name, such as charge_rescue.
    • charge retrying; charge given up: retries exhausted, or the other cause, failure or abnormal termination; and charge given up: FailWorkflow from fraud_check when a hook of the policy fraud_check gave it up.
    • charge rescuing with charge_rescue and charge rescued by charge_rescue.
    • journey succeeded, or journey succeeded, decided by early_exit when a hook of the policy early_exit decided it; journey failed at charge: declined, with the code of the reason the journey failed for, or the error's message when there is no reason; and journey aborted at charge: step could not be built, with the abort reason, or journey aborted: reporter failed when the abort concerns no step.

    A refused journey has no events, so its path is empty.

    Returns readonly string[]