Ugrás a fő tartalomra
← ⚡ JavaScript & the browser·Modul C8 · Lecke 3
FeladatBuild tests = [], test(name, fn), runAll(). Register 3 tests: 'adds' asserting 1+1===2; 'subtracts' asserting 5-3===2; 'fails on purpose' asserting 1===2. Call runAll(). Output should show 2 pass, 1 fail.

Roll-your-own test runner

150 XP10 min
Elmélet

test() collects, runAll() reports

A real test runner is two functions and an array:

const tests = [];
function test(name, fn) {
  tests.push({ name, fn });
}

function runAll() {
  let passed = 0, failed = 0;
  for (const { name, fn } of tests) {
    try {
      fn();
      console.log("PASS " + name);
      passed++;
    } catch (err) {
      console.log("FAIL " + name + " — " + err.message);
      failed++;
    }
  }
  console.log(passed + " passed, " + failed + " failed");
}

That's it. test("foo works", () => { assertEqual(...) }) registers a test. runAll() executes them all, recovers from each failure, logs the result.

The same shape Vitest / Jest expose

The famous frameworks add: parallel execution, watch mode, snapshot diffing, coverage. But the core API — test(name, fn) and "run all and report" — is what you just built.

🔒

Regisztrálj, hogy elkezdj programozni

Az elmélet mindenkinek nyitva. A szerkesztő, élő előnézet és ellenőrzés 7 napos ingyenes próbával nyílik meg — kártya szükséges, bármikor lemondható.

Regisztráció — ingyenes próba →

Minden útvonal első 10 leckéje ingyenes. Nem kell kártya.

← ElőzőKövetkező lecke →

Egy Python- vagy webes tipp naponta — emailben

Rövid, kézzel írt, spam nélkül. Egy kattintással leiratkozhatsz.