Code Reference

Coverage

Jest · Reference cheat sheet

Coverage

Jest · Reference cheat sheet


📋 Overview

Jest ships coverage via Istanbul/V8 providers. Collect with --coverage, set thresholds, and emit HTML/LCOV for CI.

🔧 Core concepts

OptionRole
--coverageEnable
collectCoverageFromInclude globs
coverageThresholdFail under %
coverageProviderbabel | v8
coverageReportershtml, text, lcov

Ignore generated files and stories in coveragePathIgnorePatterns.

💡 Examples

CLI:

npx jest --coverage
npx jest --coverage --collectCoverageFrom='src/**/*.{ts,tsx}'

Config thresholds:

module.exports = {
  collectCoverageFrom: [
    'src/**/*.{js,jsx,ts,tsx}',
    '!src/**/*.d.ts',
    '!src/**/*.stories.*',
  ],
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80,
    },
  },
  coverageReporters: ['text', 'lcov', 'html'],
};

CI:

npx jest --ci --coverage --maxWorkers=2

Open coverage/lcov-report/index.html locally.

⚠️ Pitfalls

  • Inflated coverage from trivial barrel files.
  • V8 vs babel provider differences on ignored lines.
  • Thresholds on empty suites passing vacuously.
  • Not gitignoring coverage/.
  • Collecting from dist/ instead of src/.

On this page