Testing a whole journey
What you want to do
You want to run one order through the shop's real steps and policies, from start to end, and check the way its journey went: which steps succeeded, which failed and why, and how the journey ended. The bank and the carriers should be fakes, so the test never reaches the outside world.
The code
The program is chapter 26's shop as a module, with its tests in a file beside it, as in chapter 27, and the file quiets console.log as chapter 29's does. The tests bring their own bank, which charges or is down, and their own carrier, which collects every parcel or none, implementing Payments and Carrier as chapter 26 declared them, and throwing the error of chapter 12:
/**
* A bank for tests: it charges every card, or its network is down, and has
* replaced no card.
*/
class TestBank implements Payments {
constructor(private readonly answer: "charges" | "is down") {}
async charge(order: string): Promise<string | undefined> {
if (this.answer === "is down") throw new NetworkDown();
return `R-${order}`;
}
async replacement(): Promise<string | undefined> {
return undefined;
}
}
/** A carrier for tests: it books every parcel, and collects every parcel or none. */
class TestCarrier implements Carrier {
constructor(private readonly answer: "collects all" | "collects none") {}
async book(order: string): Promise<string> {
return `T-${order}`;
}
async collects(): Promise<boolean> {
return this.answer === "collects all";
}
}Each test runs an order through pathOf, which makes an instance of the shop's workflow, as the shop does, with a second argument to instance: the instance's own reporters, which it adds after those its workflow lists. Here it holds a RecordingReporter, from itinera/testing, which keeps every event of the journey, and whose path gives the journey's path once it has ended. fakeProvider, also from itinera/testing, then names one of the workflow's providers, bank, from chapter 7, or carriers, from chapter 13, both as chapter 23 shows them, with the type token of the service it provides, PAYMENTS or CARRIER, as chapter 7 shows them, and gives a function the executor calls in its place. That function is handed provide, which makes what it returns, as a provider's this.provide(...) does:
/**
* Runs one of Ann's orders, of 30, with a parcel of this weight, through the
* shop's real steps and policies, with the test's bank and carriers in place
* of the shop's, and gives back its journey's path.
*/
async function pathOf(
order: string,
weight: number,
bank: TestBank,
carrier: TestCarrier,
): Promise<readonly string[]> {
const recorder = new RecordingReporter();
const instance = Orders.build("email").instance(
{
order,
customer: "Ann",
price: 30,
parcel: { weight, size: [20, 15, 5] },
card: "ann-2028",
giftCard: false,
},
{ reporters: [recorder] },
);
fakeProvider(instance, "bank", PAYMENTS, (provide) => provide(bank));
fakeProvider(instance, "carriers", CARRIER, (provide) => provide(carrier));
await new LocalExecutor().run(instance);
return recorder.path;
}The shop approves the path of each kind of order once, and writes it down. The paths follow the workflow as chapter 23 declared it, with outage from chapter 17. Its pieces are as chapter 26 shows the charge, its rescue step, the shipping, notify and failedShipping, chapter 22 the lookup, alreadyCompleted and the log, chapter 20 the pricing and the audit, and chapter 11 the desk. Each test compares a journey's path with the approved one:
test("an order the bank charges and the carrier takes is shipped", async () => {
assert.deepEqual(
await pathOf(
"A-1",
2,
new TestBank("charges"),
new TestCarrier("collects all"),
),
[
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 succeeded",
"journey succeeded",
],
);
});
test("a parcel over the limit is given up after its first attempt", async () => {
assert.deepEqual(
await pathOf(
"A-2",
90,
new TestBank("charges"),
new TestCarrier("collects all"),
),
[
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 failed: over-parcel-limit",
"ship given up: FailWorkflow from failedShipping",
"journey failed at ship: no-carrier-can-take-it",
],
);
});
test("a charge whose bank is down is given up and never rescued", async () => {
assert.deepEqual(
await pathOf(
"A-3",
2,
new TestBank("is down"),
new TestCarrier("collects all"),
),
[
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 terminated abnormally: the payment network is down",
"charge given up: FailWorkflow from outage",
"journey failed at charge: payment-network-down",
],
);
});
test("a parcel no carrier collects is tried once more, then given up", async () => {
assert.deepEqual(
await pathOf(
"A-4",
60,
new TestBank("charges"),
new TestCarrier("collects none"),
),
[
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 failed: too-heavy",
"ship retrying",
"ship attempt 2 failed: too-heavy",
"ship given up: retries exhausted",
"journey failed at ship: too-heavy",
],
);
});node --test prints:
✔ an order the bank charges and the carrier takes is shipped (18.378348ms)
✔ a parcel over the limit is given up after its first attempt (8.910327ms)
✔ a charge whose bank is down is given up and never rescued (3.433724ms)
✔ a parcel no carrier collects is tried once more, then given up (6.167455ms)
ℹ tests 4
ℹ suites 0
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 257.787465How it works
- A fake provider replaces only the provider's function. The workflow keeps its providers' keys and scopes. The steps still ask for
paymentsandcarrier, andcarriersstays a provider called for every attempt. Only what the provider returns changes, and only for the journey of the instancefakeProviderwas given. - Everything else is real. The steps run their own rules: the shipping refuses the parcel of 90 kg itself, with
over-parcel-limit, as chapter 27's test showed. The policies decide, the adapter supplies the weight, and the result and the events are the ones a real order would give. - A path is the journey's flow, one line for each event of it.
pathreads only the events that say how the flow went: each attempt's outcome, each decision about a step, and how the journey ended. Everything else, such as which provider served a step or which data was committed, is left out, so a path changes only when the flow does. - Each line is a plain sentence.
charge attempt 1 terminated abnormallynames the step, its attempt and how it ended, with the reason or the error after a colon.ship given up: FailWorkflow from failedShippingnames the cause, and the policy that decided it.journey failed at ship: no-carrier-can-take-itnames the step where the journey ended, and the reason it ended with: after retries are exhausted, the last attempt's reason,too-heavyfor the parcel no carrier collects. - An approved path is a decision of the flow, written down. A person reads it once and agrees that this is what should happen to such an order. If a change to the workflow sends an order another way, the test fails. A change to a step's rules that keeps the flow keeps the path.
- The test awaits the journey, as the shop does, with an executor of its own, and reads the path once the journey has ended.
So the shop tests each piece alone, its flow with scripts, and whole journeys by their paths. But the shop's log will soon write to a logging service, over the network too, where a write can fail.
Outcomes
We have shown how to test a whole journey, with fake providers in place of the workflow's, by comparing its path with an approved one.
| The usual way | With Itinera |
|---|---|
| Testing a whole request often needs a test double for every dependency, wired by hand into the code under test. | fakeProvider replaces a workflow's provider by name, and the steps get the fake as they would the real service. |
| An end-to-end test usually checks the final state, and says little about the way a request went. | A journey's path lists each outcome and decision in order, and a test compares it with the path the shop approved. |