Assertions: What a Test Actually Proves
Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.
What you will be able to do
#- Describe a test as setup, action and one or more assertions.
- Predict whether a given assertion passes or fails and why.
- Explain why a passing test is evidence, not proof.
Overview
#A test is a claim with evidence
A test is a small program that makes a claim about behavior and then checks the claim. The check is an assertion: a comparison between what the code actually produced and what the author expected. If the comparison holds, the assertion passes. If it does not, the assertion throws and the test fails. The failure message is the evidence that something changed.
function assertEqual(actual, expected, label) {
if (!Object.is(actual, expected)) {
throw new Error(label + ': expected ' + expected + ' but got ' + actual);
}
}
function add(a, b) {
return a + b;
}
assertEqual(add(2, 3), 5, 'adds two positives');
Nothing here is magic. A test runner is a program that collects test functions, runs them, catches thrown errors and reports which ones threw. You can build a minimal runner yourself, and doing so removes the mystery from the tools you use later.
Setup, action, assertion
Every useful test has three parts, even when they are not labeled. Setup creates the world the test needs. Action calls the code under test. Assertion states the expected result. When a test is hard to read, it is usually because setup and action are tangled together, so the reader cannot tell which line produced the observed value.
What a passing test does not prove
A passing test proves only that the assertions you wrote held for the inputs you supplied. It does not prove the function is correct for other inputs. It does not prove the function is free of side effects. It does not prove the requirement was understood correctly. A test that asserts add(2, 3) === 5 passes even if add is wrong for negative numbers. This is why tests come in sets, and why edge cases matter.
Failure is the product
A test that never fails is a test that never taught you anything. The value of a test is the failure it can produce: when the behavior regresses, the test turns red and points at the broken claim. Write the assertion first, watch it fail for the right reason, then make it pass. A test you have never seen fail is a test you cannot yet trust.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Given the assertEqual helper from the lesson, what does this script print?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptassertEqual(0.1 + 0.2, 0.3, 'float sum'); console.log('done');Exercise 2Explain
Explain why a test suite can be fully green while the application still has a bug.
Reveal the reference answer
reference.javascriptA test only checks the assertions the author wrote for the inputs the author chose. If no test covers the input that triggers the bug, the suite stays green because it never exercises that path. Green means 'no known claim was violated', not 'the program is correct'. Closing the gap requires adding the missing case, not trusting the color.The answer should separate the claim a test makes from the claim people assume it makes. A strong answer names the missing input or path as the reason for the blind spot.
Exercise 3Implement
Implement `assertThrows(fn, message)` that returns true when calling `fn` throws and false when it does not. `console.log(assertThrows(() => { throw new Error('x'); }, 'throws'), assertThrows(() => 1, 'no throw'))` should print `true false`.
starter.javascriptfunction assertThrows(fn, message) { // your code here }Reveal the reference answer
reference.javascriptfunction assertThrows(fn, message) { try { fn(); return false; } catch (error) { return true; } }Authored expected output: true false
The helper calls the function inside a try block. If the call completes without throwing, the function did not throw, so it returns false. If it throws, control jumps to catch and the helper returns true.
Exercise 4Debug
This test always passes even when `divide` returns the wrong value. Fix the assertion so it can actually fail.
starter.javascriptfunction divide(a, b) { return a / b; } const result = divide(10, 2); if (result) { console.log('pass'); }Reveal the reference answer
reference.javascriptfunction divide(a, b) { return a / b; } const result = divide(10, 2); if (result === 5) { console.log('pass'); } else { throw new Error('expected 5 but got ' + result); }Authored expected output: pass
A truthiness check passes for any non-zero number, so it cannot detect a wrong value. Comparing against the exact expected value and throwing otherwise turns the check into a real assertion.
Exercise 5Test
Write assertions that prove `add` handles positive numbers, negative numbers and zero.
starter.javascriptfunction add(a, b) { return a + b; }Reveal the reference answer
reference.javascriptfunction assertEqual(actual, expected, label) { if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual); } assertEqual(add(2, 3), 5, 'positives'); assertEqual(add(-2, -3), -5, 'negatives'); assertEqual(add(0, 0), 0, 'zeros'); console.log('add verified');Authored expected output: add verified
Three assertions cover three input classes. Each throws with a distinct label so a failure names the class that broke, which is far more useful than a single pass or fail.
Exercise 6Interview
In an interview, explain what a failing test tells you and what it does not tell you.
Reveal the reference answer
reference.javascriptA failing test tells me that a specific claim I wrote about behavior is no longer true for a specific input. It narrows the search to the code path that assertion exercises. It does not tell me the root cause, whether the test itself is wrong, or whether other untested paths are also broken. I treat the failure as a clue, reproduce it, and only then decide whether the code or the expectation is the defect.The answer should treat failure as bounded evidence and mention the possibility that the test, not the code, is wrong.
Common misconceptions
#- A green test suite means the code is correct.
- An assertion is only a log statement that prints the expected value.
- Tests should be written after all code is finished and never run while failing.
Follow-up practice
#- testing-structure-and-isolation
- testing-unit-pure-functions
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.