Labs ICT
Pro Login

Test Coverage

How much of your code is actually tested.

Test Coverage

So you have a bunch of tests, but how do you know if you are testing enough? Test coverage tells you what percentage of your code your tests actually exercise. It is not everything, but it is a useful metric.

Here is the thing. High coverage does not mean your code is well tested. But low coverage definitely means your code is not tested enough.

Running Coverage Reports

Jest has a built-in coverage flag. Just run it from your terminal.


npx jest --coverage
    

This gives you a report showing how many lines, functions, branches, and statements your tests cover.

Understanding the Numbers

Coverage reports show four metrics. Lines means how many lines of code ran during tests. Functions means how many functions got called. Branches means how many if/else paths you hit. Statements means how many statements executed.


// jest.config.js
module.exports = {
  coverageThresholds: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    }
  }
};
    

Setting thresholds in your config means Jest fails if coverage drops below those numbers. That prevents your coverage from slowly degrading over time.

What to Cover

Focus on covering your business logic first. That means validation functions, calculations, data transformations, and conditional logic. Utility functions and simple getters can have lower coverage.

Coverage Is Not Everything

I have seen code with 100 percent coverage that was still buggy. Coverage tells you what code ran, not whether it was tested correctly. A test that calls a function but never asserts anything still counts toward coverage. Use coverage as a guide, not a goal.

Try it Yourself →

Key Takeaways

  • Coverage reports show what percentage of code your tests exercise
  • Use jest --coverage to generate a report
  • Set coverage thresholds to prevent regression
  • Focus coverage on business logic, not trivial code
  • High coverage does not guarantee well-tested code