Μετάβαση στο κύριο περιεχόμενο
← ⚡ JavaScript & the browser·Ενότητα C8 · Μάθημα 9
ΕργασίαWrite assertEqual(actual, expected, msg) producing the multi-line message: '<msg>\n expected: <json>\n actual: <json>'. Trigger a failure with assertEqual({a:1}, {a:2}, 'objects equal') in try/catch, log err.message.

Helpful failure output

100 XP7 min
Θεωρία

A failing test should tell you everything

When a test fails, the message should answer: what did we expect, what did we get, and where? Bad failure messages cost minutes per debug session — multiplied by hundreds of failing CI runs per year, the cost compounds.

function assertEqual(actual, expected, msg) {
  if (actual !== expected) {
    throw new Error(
      `${msg || "values differ"}\n  expected: ${JSON.stringify(expected)}\n  actual:   ${JSON.stringify(actual)}\n`
    );
  }
}

Good vs bad

BAD:  Error: assert failed
GOOD: Error: cart total
        expected: 225
        actual:   220

The good version tells you WHICH assert, WHAT was expected, and WHAT actually came out — you can fix it without re-running in a debugger.

🔒

Εγγράψου για να αρχίσεις να προγραμματίζεις

Η θεωρία είναι ανοιχτή για όλους. Ο επεξεργαστής, η ζωντανή προεπισκόπηση και ο έλεγχος ξεκλειδώνουν με 7-ήμερη δωρεάν δοκιμή — απαιτείται κάρτα.

Εγγραφή — δωρεάν δοκιμή →

Τα πρώτα 10 μαθήματα κάθε διαδρομής είναι δωρεάν. Δεν χρειάζεται κάρτα.

← ΠροηγούμενοΕπόμενο μάθημα →

Μια συμβουλή Python ή web την ημέρα — με email

Σύντομο, γραμμένο στο χέρι, χωρίς spam. Διαγραφή με ένα κλικ.