# Coverage and Its Limits — Jest, Vitest & Testing Library

Source: https://www.skillbyai.com/en/javascript-testing/x-coverage

> v8 versus istanbul, and what the numbers do not say.

## Coverage shows what ran, not what was checked

Coverage tools report which **statements, branches, functions and lines** executed during tests. There are two main approaches: **istanbul** instruments your source code before running it, while **v8** uses the coverage data the V8 engine collects natively, which is usually faster and needs no instrumentation step. In Vitest you choose `coverage.provider: 'v8'` or `'istanbul'` and install the matching `@vitest/coverage-*` package; Jest uses `coverageProvider: 'babel'` (istanbul-based, the default) or `'v8'`. Accuracy of the two has converged in recent releases; check the docs for your version. Remember the limit: a line can be executed by a test that asserts nothing about it. 100% coverage does not mean correct behaviour, and chasing it produces low-value tests. Use coverage to **find untested areas**, and set modest thresholds to stop it sliding.

## Coverage configuration and commands

Vitest config shown; Jest options in the comment.

```typescript
// vitest.config.ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    coverage: {
      provider: 'v8',                        // or 'istanbul' (npm i -D @vitest/coverage-v8 / -istanbul)
      reporter: ['text', 'html', 'lcov'],
      include: ['src/**/*.{ts,tsx}'],
      exclude: ['src/**/*.test.{ts,tsx}', 'src/test/**', 'src/**/*.d.ts'],
      thresholds: { lines: 80, branches: 70 },   // fail the run if coverage drops below
    },
  },
});

// run:  npx vitest run --coverage
// Jest: collectCoverage / --coverage, coverageProvider: 'v8' | 'babel',
//       coverageThreshold: { global: { lines: 80, branches: 70 } }
```

## Look at uncovered branches, not the percentage

The HTML report highlights branches never taken, such as error paths and empty states. Those are exactly the cases worth a test; the overall percentage is just a trend line.

**Quiz:** What does 100% line coverage guarantee?

- [ ] The code has no bugs
- [x] Every line ran during the tests, not that its behaviour was checked
- [ ] Every branch and edge case was asserted
- [ ] The UI is accessible

*Answer:* Every line ran during the tests, not that its behaviour was checked. Coverage measures execution, not verification.
