Why don't green tests mean the code works?
Tests only check what you thought of. Coverage, mutants, and tests that repeat the bug.
A test proves exactly one thing: on these inputs the code gives this answer. A green suite means "the cases I thought of work". And bugs live precisely in the cases nobody thought of.
function average(numbers) {
if (numbers.length === 0) return 0;
let sum = 0;
for (const n of numbers) sum += n;
return Math.round(sum / numbers.length); // a stray rounding
}
console.log(average([2, 4]) === 3); // the only test: green
console.log(average([1, 2])); // 2 instead of 1.5There's a metric called coverage: vitest --coverage counts which lines of code ran during the tests. 100% sounds like a guarantee, but it only means "every line ran", not "every line was checked". A test without expect produces coverage and catches nothing.
function shippingCost(total) {
return total > 3000 ? 0 : 299; // the rule says "from 3000": should be >=
}
// "Tests": both branches ran, so coverage is 100%
shippingCost(5000);
shippingCost(500);
console.log("checks: 0, and an order of exactly 3000 costs", shippingCost(3000));The most honest measure of test strength is mutation testing. A tool (for JavaScript, Stryker) creates hundreds of mutants on its own: swaps > for >=, + for -, true for false, deletes lines, and runs your tests on each. If at least one test fails, the mutant is "killed". If everything stays green, the mutant "survived", and you have a blind spot: your tests won't notice that kind of bug. The share of killed mutants is the mutation score.
Another trap is a test that repeats the implementation. If the code says price * 0.9 and the test says expect(final(price)).toBe(price * 0.9), a mistake in the formula gets copied into the test, which stays green. Expected values come from the requirements and are worked out by hand: "1000 becomes 900". AI-generated tests often fall into exactly this trap: they "photograph" what the code does right now, bugs included.
On the bright side, good tests are the best documentation. Names like "exactly 3000 already ships free" and "you can't spend zero" are requirements that can't be forgotten or go stale: the moment the code drifts away from them, the test turns red.
Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.