Function fakeProvider

  • Puts fake in place of the function of the workflow's service provider named name, for the journey of instance alone, replacing any fake given before for that provider, and returns instance.

    Only the provider's function is replaced. It keeps its scope, the steps it serves and what it requests: the executor still resolves its requests first, so missing data aborts the journey as without a fake, then calls fake wherever it would call the provider, once per journey or once per attempt. fake returns what provide makes, or a promise of it, and a fake that throws, or rejects, aborts the journey as the provider would. service_provided names the implementation fake made. The workflow's declaration, its admission and its listing are unchanged.

    import assert from "node:assert/strict";
    import { LocalExecutor } from "itinera/executor";
    import { type Provided, provider, service, serviceType } from "itinera/service";
    import { type Outcome, Step, step } from "itinera/step";
    import { fakeProvider } from "itinera/testing";
    import { withStep, Workflow, workflow } from "itinera/workflow";

    interface PaymentGateway {
    take(amount: number): boolean;
    }

    const PAYMENT_GATEWAY = serviceType<PaymentGateway>("PaymentGateway");

    @step()
    class Charge extends Step {
    override run(
    @service("payments", PAYMENT_GATEWAY) payments: PaymentGateway,
    ): Outcome {
    return payments.take(40) ? this.succeed() : this.fail("declined");
    }
    }

    @workflow({ name: "orders" })
    @withStep(Charge, { name: "charge" })
    class Orders extends Workflow {
    @provider(PAYMENT_GATEWAY, { name: "live gateway" })
    payments(): Provided<PaymentGateway> {
    return this.provide({ take: () => true });
    }
    }

    export default (async () => {
    const instance = fakeProvider(
    Orders.build().instance({}),
    "live gateway",
    PAYMENT_GATEWAY,
    (provide) => provide({ take: () => false }, { fake: "declining" }),
    );

    const result = await new LocalExecutor().run(instance);

    assert.ok(result.isFailed());
    assert.equal(result.failure?.reason?.code, "declined");
    })();

    Type Parameters

    • W extends Workflow
    • T

      The type of the service.

    Parameters

    Returns WorkflowInstance<W>

    TestMistake when the workflow has no service provider named name, or when that provider makes another service than service.