Skip to content

datfinesoul/coverage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coverage vs Correctness Demo

This repository demonstrates that 100% code coverage does not guarantee bug-free code.

The Problem

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.

The Example

This repo contains a simple average() function with tests that achieve 100% coverage, yet the code still has bugs.

What the tests cover:

  • ✅ Positive numbers
  • ✅ Single number
  • ✅ Empty array
  • ✅ Every line of code
  • ✅ Every branch

What the tests miss:

  • ❌ Non-numeric values: average([1, 2, "oops"]) returns NaN
  • ❌ Null values: average([1, 2, null, 3]) returns 1.5 (wrong!)
  • ❌ Undefined values
  • ❌ Mixed valid/invalid data

Setup Instructions

1. Clone or download this repository

2. Install dependencies:

npm install

3. Run the tests:

npm test

4. Run tests with coverage report:

npm run test:coverage

You'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:

node
const { average } = require('./math');
average([1, 2, "oops", 3]); // Returns NaN
average([1, 2, null, 3]);   // Returns 1.5 (should be 2)

The Lesson

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

Good testing requires:

  1. High coverage (execute all code paths)
  2. Edge case testing (null, undefined, empty, boundary values)
  3. Invalid input testing (wrong types, malformed data)
  4. Business logic validation (correct behavior for all scenarios)

Coverage is a useful metric, but it's just one piece of the testing puzzle.

Files in This Repo

  • math.js - The function with hidden bugs
  • math.test.js - Tests that achieve 100% coverage but miss bugs
  • package.json - Dependencies and scripts
  • README.md - This file

About

Coverage vs Correctness Demo

Resources

Stars

Watchers

Forks