Command Palette

Search for a command to run...

Hectal
Phase 13: Errors, Debugging and Testing
JavaScript · Exercise 5 of 5 Previous
Intermediate

Build a tiny test runner and fix a bug with it

Build a small test kit, then use it to find and fix a bug in cartTotal.

1. assertEqual(actual, expected) does nothing when the values are deeply equal (compare JSON.stringify of both) and throws an Error with a message like expected 0 but got ... when they aren't.

2. test(name, fn) runs fn. If it doesn't throw, log PASS <name>; if it throws, log FAIL <name>: <message>. Also add an <li> with the same text to #report. A failing test must not stop the others.

3. Write at least three tests for cartTotal: two items, one item, and an empty cart (which should be 0). Some will fail. Fix cartTotal so they all pass.

4. After the tests, write <passed> passed, <failed> failed in #summary.

What Check tests

  1. At least three tests are listed in #report, all passing
  2. The summary says how many passed and that 0 failed
  3. assertEqual stays silent for equal values, arrays included
  4. assertEqual throws for different values
  5. test logs PASS <name> for a passing test
  6. test logs FAIL <name>: <message> and doesn't throw
  7. cartTotal is fixed

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.