JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstesting
PrevNext
testing
mid
coverage

What is code coverage and how do you interpret coverage reports?

code-coverage
jest
testing-metrics
quality
Quick Answer

Code coverage measures how much source code is executed by tests. Metrics include lines, statements, branches, and functions. High coverage doesn't guarantee quality - focus on testing critical paths and edge cases rather than chasing 100%.

Detailed Explanation

Coverage Metrics:

  • Line coverage: Lines executed
  • Statement coverage: Statements executed
  • Branch coverage: Conditional paths taken
  • Function coverage: Functions called

Interpreting Results:

  • Red: Not covered
  • Yellow: Partially covered (branches)
  • Green: Fully covered

Practical Guidelines:

  • 80% is a reasonable target
  • Focus on critical code paths
  • Don't sacrifice test quality for coverage
  • Use coverage to find untested areas

Limitations:

  • Coverage doesn't measure assertion quality
  • 100% coverage can still have bugs
  • Generated code inflates numbers

Code Examples

Running coverage in Jest
# Run tests with coverage
npm test -- --coverage

# Or configure in package.json
{
  "scripts": {
    "test": "jest",
    "test:coverage": "jest --coverage"
  }
}

# Jest config for coverage thresholds
// jest.config.js
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    },
    // Per-file thresholds
    './src/utils/': {
      branches: 90,
      functions: 90
    }
  },
  collectCoverageFrom: [
    'src/**/*.{js,jsx}',
    '!src/**/*.test.{js,jsx}',
    '!src/index.js'
  ]
};

Resources

Jest Coverage

docs

Related Questions

What is the difference between unit tests and integration tests?

mid
integration

How do you mock functions, modules, and API calls in Jest?

mid
mocking
Previous
What is the difference between unit tests and integration tests?
Next
How do you test custom React hooks?