See What a Green Run Leaves Out

In the last lesson, you read Edsger’s pricing tests as examples, each one a given/when/then statement, and watched npm test report 15 passes. That report is often the first thing a reviewer looks at because a green run feels like a verdict.

The shop’s desk staff have a problem with one of the refund messages, and Edsger has a fix. In this lesson, you’ll review that change and decide whether you’d approve it. You’ll also learn to say exactly what a passing run shows. Start with what the desk staff need.

Try a Change That Passes Every Test

The change below is hypothetical: it’s made up for this lesson, and the repository has no branch or pull request for it. Imagine Edsger proposes it with this description:

The desk staff can’t tell “this book was already sent back” from “the customer asked for more copies than they bought”. Both show “Already returned”. This splits the check in returnedLines() into two, with a clearer message for each.

The check lives in src/refunds/lines.js. returnedLines() finds the order line for each book in a return, then decides whether the return fits. Here are lines 16 and 17:

Language: JavaScript
        let already = found.returned || 0;
        if (already + item.quantity > found.quantity) throw new Error('Already returned');

Line 16 stores the number of copies already returned, and line 17 refuses the return if the combined total exceeds the quantity ordered. Edsger’s proposal replaces line 17 with two lines:

Language: File Changes (diff)
-        if (already + item.quantity > found.quantity) throw new Error('Already returned');
+        if (already >= found.quantity) throw new Error('Already returned in full');
+        if (item.quantity > found.quantity) throw new Error(`Only ${found.quantity} in the order`);

The first new line refuses a return once every copy is back. The second refuses one that asks for more copies than the line held, and names the number. Both tell the desk what went wrong, as the description promised.

Try it in your own copy. In your second-draft-books folder, on main, open src/refunds/lines.js. Delete line 17 and type the two lines that start with + in its place, without the +. Then run the suite:

Language: Shell
$ npm test

> [email protected] test
> node --test

✔ test/helpers.js (45.61771ms)
...
✔ returning a single paid book restores its stock and payment (2.68097ms)
✔ the final paid return receives the remaining rounding penny (0.54764ms)
...
ℹ tests 15
ℹ suites 0
ℹ pass 15
ℹ fail 0
...

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.