This repository demonstrates that 100% code coverage does not guarantee bug-free code.
Code coverage tools measure which lines of code are executed during tests, but they don't measure whether your code handles all possible inputs correctly.
This repo contains a simple average() function with tests that achieve 100% coverage, yet the code still has bugs.
- ✅ Positive numbers
- ✅ Single number
- ✅ Empty array
- ✅ Every line of code
- ✅ Every branch
- ❌ Non-numeric values:
average([1, 2, "oops"])returnsNaN - ❌ Null values:
average([1, 2, null, 3])returns1.5(wrong!) - ❌ Undefined values
- ❌ Mixed valid/invalid data
1. Clone or download this repository
2. Install dependencies:
npm install3. Run the tests:
npm test4. Run tests with coverage report:
npm run test:coverageYou'll see 100% coverage across all metrics:
----------|---------|----------|---------|---------|
File | % Stmts | % Branch | % Funcs | % Lines |
----------|---------|----------|---------|---------|
All files | 100 | 100 | 100 | 100 |
math.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|
5. View detailed HTML coverage report:
open coverage/lcov-report/index.html(On Windows: start coverage/lcov-report/index.html)
6. Try the bug yourself:
Open Node.js REPL and run:
nodeconst { average } = require('./math');
average([1, 2, "oops", 3]); // Returns NaN
average([1, 2, null, 3]); // Returns 1.5 (should be 2)Code coverage measures execution, not correctness.
- Coverage tells you what code ran during tests
- Coverage does NOT tell you if your code handles edge cases
- Coverage does NOT tell you if your logic is correct
- Coverage does NOT tell you if you tested the right scenarios
- High coverage (execute all code paths)
- Edge case testing (null, undefined, empty, boundary values)
- Invalid input testing (wrong types, malformed data)
- Business logic validation (correct behavior for all scenarios)
Coverage is a useful metric, but it's just one piece of the testing puzzle.
math.js- The function with hidden bugsmath.test.js- Tests that achieve 100% coverage but miss bugspackage.json- Dependencies and scriptsREADME.md- This file