JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstesting
PrevNext

Learn the concept

Code Coverage Metrics

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 JestBash
# 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'
  ]
};

Real-World Applications

Use Cases

Identifying untested areas in a legacy codebase before a major refactor

Running code coverage reports to visualize which parts of the code are not exercised by existing tests, guiding the creation of new tests for critical or high-risk sections.

Setting up quality gates in a CI/CD pipeline for a critical microservice

Configuring coverage thresholds (e.g., 80% lines, 75% branches) to ensure that new code merges don't drop the overall test coverage, maintaining a baseline of test quality.

Reviewing a pull request for a new feature to ensure adequate test coverage

Analyzing the coverage report for the changed files to verify that the new functionality and its edge cases are sufficiently tested, rather than just looking at the overall project coverage.

Mini Projects

Coverage Report Generation and Analysis

intermediate

Take a small JavaScript project with existing tests. Generate a code coverage report using Jest and analyze the report to identify areas with low coverage, then write new tests to improve it.

CI/CD with Coverage Thresholds

advanced

Set up a GitHub Actions workflow for a project that runs tests and generates a coverage report. Configure a step to enforce a minimum coverage threshold, causing the build to fail if the threshold isn't met.

Industry Examples

Google (Chromium)

The Chromium project uses extensive code coverage analysis to ensure the stability and reliability of its browser engine. High coverage, combined with other quality metrics, helps manage the immense complexity and continuous development of the codebase.

Microsoft (Azure SDKs)

Microsoft's Azure SDKs are developed with a strong emphasis on reliability. Code coverage is a key metric in their CI/CD pipelines to ensure that the SDKs are thoroughly tested before release, minimizing bugs for developers using their cloud services.

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?
PrevNext