Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Watch Your Test Fail First

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.js keeps the report short. On refund-tests, which starts from main, Nadia’s bug is still in the code, so four tests pass and returning Small Maps from Nadia's order refunds 600p fails.
  • 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: 450 is Nadia’s short refund from issue #41, and expected: 600 is 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.js itself, 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 s back if you tried the typo. Then change line 4 of src/refunds/allocation.js to divide by order.paidItemsSubtotal, the last course’s one-line fix. git status lists 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.js undoes 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:

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

00:00 Your refund-tests branch holds three new tests, one for each criterion of issue 41 that the suite didn’t cover. You’ve committed them, but you haven’t run them yet.

00:11 Say your 600p test passed on its first run. That wouldn’t tell you much. A test you’ve never seen fail might never reach the refund, or it might check something that’s always true.

00:23 So before you trust it, watch it fail.

00:28 The branch started from main, so Nadia’s bug is still in the code. Run only the refund file for now, so the report stays short: node --test, then its path.

00:40 Four tests pass. Your 600p test fails.

00:45 Below the counts, the report explains the failure. It names the test and the line where it starts, 57. Then the assertion that failed, on line 62: 450 is not equal to 600.

00:59 The lines underneath say which is which: actual 450, expected 600. That’s the right reason. The test reached the refund and got 450p, Nadia’s short refund from the issue.

01:14 Your criterion says 600p, and that’s the value the test expected.

01:20 Not every red run means that. Say you’d typed one import path wrong, refund without the s. Your turn: pause the video and predict. With this typo, how many of the five tests run?

01:39 None of them. Node can’t find the module, so it can’t load the file. The report counts one failure, the file itself. The refund code never runs.

01:52 So a red run isn’t enough. Read the message, and check that it names the values your criterion predicts.

02:00 If you tried that typo, put the s back. Now make it pass. You fixed this bug in the last course, on a branch called fix-refund. This branch started from main, so the fix isn’t here.

02:13 Re-apply its one line. Line 4 of the allocation file divides by the gross subtotal, the catalogue value of every book. Change it to the paid-items subtotal, the books Nadia paid for.

02:26 git status shows one modified file, the allocation file, so the fix is the only change. Run the tests again. All five pass.

02:39 Often the fix is already in by the time you write a test, so there’s no red run to watch. Then you see it fail by taking the fix out. Try that here, where you know the answer.

02:50 Undo the line with git restore, and run the tests again. The same test fails, with the same 450 and 600. It fails with the bug and passes without it, so now it’s evidence.

03:05 Look at the first test, Edsger’s ordinary return. It passed every time it ran. Its order is one Small Maps with no code, so both subtotals are 750. Dividing by either one gives a 750p refund.

03:20 That test can’t tell the bug from the fix, which is why criterion 1 needed a test of its own. Put the line back and run once more to see five passes. Before you commit, run the whole suite with npm test, since a change to the shop’s code can reach past this one file.

03:38 That’s Edsger’s tests and your three, 18 of 18. Then commit.

03:44 Your two 2400p tests passed every time too. This bug refunds too little, so it never pushes the total refunded over 2400p, and it never changes what Nadia paid.

03:58 They aren’t evidence yet. Seeing them fail takes code broken a different way. Note that in map.md.

04:06 So before you trust a new test, see it fail with the values its criterion predicts. Then see it pass. Next, in Exercise: Write Tests That Catch Mistakes, you’ll write tests for another of the shop’s rules, and check that each one catches a broken version.

Become a Member to join the conversation.