Sub topic 12
Testing the paths through a flow
This chapter assumes you have the flow model from the earlier chapters: a steps table, an advance function, a back function, and a FlowState with current, history, data, and errors. It also assumes you have written unit tests in some framework. The examples use Node's built-in test runner, but the ideas carry over to any framework.
Why flows need path-based tests
A flow is a graph. Each answer can send the user down a different route, and each route has its own validation rules. Testing one happy path leaves most of the graph unchecked. Bugs usually hide in three places:
- A branch that sends the user to the wrong step.
- A step that lets the user advance with invalid data.
- A Back action that returns to the wrong step after a branch.
Because the earlier chapters kept flow logic in plain functions and data, you can test all of this without rendering any screens.
The paths in the example flow
The example flow has two routes from the account step to review.
Two routes means at least two path tests. Each step also has validation rules, so each step needs a test for blocked advancement.
Setting up the tests
Put the test code in flow.test.ts. It assumes flow.ts exports Data, Step, FlowState, steps, advance, and back as defined earlier. Run it with npx tsx --test flow.test.ts.
import { test } from "node:test";
import assert from "node:assert/strict";
import { steps, advance, back } from "./flow";
import type { Data, FlowState } from "./flow";
function start(data: Data = {}): FlowState {
return { current: "account", history: [], data, errors: [] };
}
function pathFor(data: Data): string[] {
const ids: string[] = [];
let id: string | null = "account";
while (id !== null) {
ids.push(id);
if (ids.length > Object.keys(steps).length) {
throw new Error("Flow does not terminate: " + ids.join(" -> "));
}
id = steps[id].next(data);
}
return ids;
}
test("personal accounts skip the company step", () => {
const data = { email: "[email protected]", accountType: "personal" };
assert.deepEqual(pathFor(data), ["account", "review"]);
});
test("business accounts visit the company step", () => {
const data = { email: "[email protected]", accountType: "business" };
assert.deepEqual(pathFor(data), ["account", "company", "review"]);
});The pathFor helper follows each step's next function until the flow ends. It throws if the path is longer than the number of steps, which catches a branch that loops forever. These tests check the branching rules without any UI or state.
Testing validation gates
A gate test checks that an invalid step does not advance. It should check two things: the current step stays the same, and the errors are set.
test("an invalid email blocks the account step", () => {
const state = start({ email: "not-an-email", accountType: "personal" });
const result = advance(state, steps);
assert.equal(result.current, "account");
assert.deepEqual(result.history, []);
assert.deepEqual(result.errors, ["Enter a valid email address."]);
});
test("a blank company name blocks the company step", () => {
const state: FlowState = {
current: "company",
history: ["account"],
data: { email: "[email protected]", accountType: "business", company: " " },
errors: [],
};
const result = advance(state, steps);
assert.equal(result.current, "company");
assert.deepEqual(result.errors, ["Enter your company name."]);
});Write one blocked-advance test for every rule that has a distinct error message. Whitespace-only input, as in the second test, is a common edge case. Test it explicitly.
Testing navigation and history
Run the flow from the start to the end, then walk it backward. This checks that history stays consistent across a branch.
test("Back returns through the branch the user took", () => {
let state = start({ email: "[email protected]", accountType: "business", company: "Acme" });
state = advance(state, steps);
assert.equal(state.current, "company");
state = advance(state, steps);
assert.equal(state.current, "review");
assert.deepEqual(state.history, ["account", "company"]);
state = back(state);
assert.equal(state.current, "company");
state = back(state);
assert.equal(state.current, "account");
assert.deepEqual(state.history, []);
});
test("Back on the first step does nothing", () => {
const state = start();
assert.deepEqual(back(state), state);
});
test("advance on the last step stays put", () => {
const state: FlowState = { current: "review", history: ["account"], data: {}, errors: [] };
assert.equal(advance(state, steps).current, "review");
});The last two tests cover the boundaries: the first step and the final step. Off-by-one mistakes tend to appear at these points.
A checklist for choosing tests
Use this list to decide what to cover.
| What to test | Example in this flow |
|---|---|
| Each route from start to end | Personal and business paths |
| Each validation rule, blocked | Bad email, blank company |
| Each validation rule, passing | Valid email advances |
| Back after each branch | Back from `company` to `account` |
| Boundaries | Back on step one, advance on the last step |
| Changed answers | Switch from business to personal, then check the path |
The last row matters for real users. If someone chooses business, fills in a company, goes back, and switches to personal, the final path must skip company. Decide whether stale company data should be dropped before submission, and write a test that states your decision.
Where to go beyond unit tests
Unit tests on the logic cover most of the risk. A small number of end-to-end tests in a real browser or app can then confirm that the screens, focus handling, and error display work with the logic. Keep those few and focused on one complete path each.
Exercise
A user picks "business", fills in the company name, goes Back to the account step, and changes the type to "personal". Which test would catch a bug where the review step still shows the company name, and what should it assert?
Show answer
A changed-answers test. Start from the business path with a company filled in, go back, change accountType to "personal", then advance. It should assert that pathFor returns ["account", "review"] and that the data submitted from review does not contain the stale company value (or that your flow explicitly documents keeping it).