In this lesson, you watch the three new tests on refund-tests fail before you trust them, and check that each failure is red for the right reason.
Key Points
- A test you’ve only seen pass isn’t evidence yet. It might never reach the code it’s about, or it might check something that’s always true. Before you trust it, watch it fail.
- Run one file while you work.
node --test test/refunds.test.jskeeps the report short. Onrefund-tests, which starts frommain, Nadia’s bug is still in the code, so four tests pass andreturning Small Maps from Nadia's order refunds 600pfails. - Red for the right reason names the values your criterion predicts. The report names the test, which starts at line 57, and the assertion that failed, on line 62:
450 !== 600.actual: 450is Nadia’s short refund from issue #41, andexpected: 600is criterion 1. - A red run can fail for the wrong reason. With one mistyped import path,
'../src/refund/create.js', Node can’t find the module (ERR_MODULE_NOT_FOUND) and can’t load the file. The report counts one failure,test/refunds.test.jsitself, and none of the five tests run. So read the message, not only the color. - Re-apply the fix, and check it’s the only change. Put the
sback if you tried the typo. Then change line 4 ofsrc/refunds/allocation.jsto divide byorder.paidItemsSubtotal, the last course’s one-line fix.git statuslists only that file as modified, and all five tests pass. - No red run to watch? Take the fix out. Often the fix is already in by the time you write a test.
git restore src/refunds/allocation.jsundoes it, and the same test fails with the same 450 and 600. A test that fails with the bug and passes without it is evidence. - Some tests can’t tell the bug from the fix. Edsger’s ordinary-return test passes every time. Its order, one Small Maps with no code, has both subtotals at 750, so either one gives a 750p refund. That’s why criterion 1 needs a test of its own.
- The two 2400p tests aren’t evidence yet either. This bug refunds too little, so it never pushes the total refunded over 2400p and never changes what Nadia paid. Seeing them fail takes code broken a different way, so note that in the Tests section of
map.md. - Run the whole suite before you commit. Put the fix back, run the file once more for five passes, then run
npm test, since a change to the shop’s code can reach past this one file: Edsger’s tests and your three, 18 of 18. - The rule: before you trust a new test, see it fail with the values its criterion predicts, then see it pass.
Final Code
src/refunds/allocation.js on refund-tests, with the fix re-applied on line 4: