×

javascript · level 3 · unit 3 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A catches every failure and reports the score.

With one assertion after another, the first failure stops everything, and you never hear about the rest. A test runner wraps each test in its own try...catch, records a pass or a fail with the reason, and carries on.

Here test takes a name and a function. A group of tests like this is a . Run the suite after every change: if an old test suddenly fails, you have caught a before your users do.

const score = { passed: 0, failed: 0 };
function test(name, check) {
  try {
    if (check() !== true) { throw new Error("returned false"); }
    score.passed++;
  } catch (error) {
    score.failed++;
    console.log(`FAIL ${name} (${error.name})`);
  }
}
const shout = (text) => `${text.toUpperCase()}!`;
test("adds a bang", () => shout("hi") === "HI!");
test("copes with no text", () => shout() === "!");
console.log(`${score.passed} passed, ${score.failed} failed`);

Real runners such as Jest and Vitest work the same way, with far more helpers.