Test Coverage
- Measures how much of your code is tested.
- Tools: Jest (--coverage), Istanbul, Codecov.
Metrics:
- Line coverage – % of lines executed
- Branch coverage – % of branches tested
- Function coverage – % of functions called
Example:
jest --coverage
Output:
Statements : 92.3% ( 48/52 ) Branches : 85% ( 17/20 ) Functions : 95% ( 19/20 ) Lines : 92.3% ( 48/52 )
Flaky Tests
- Pass/fail inconsistently → hurts trust.
- Causes:
- Race conditions
- Network delays
- Random timeouts
Fixes:
- Use mocked APIs instead of live.
- Use await properly to avoid async issues.
- Retry logic in CI (Cypress: retries: 2).
CI Stability
- Run unit tests in parallel (Jest + GitHub Actions).
- Run critical-path E2E only on PRs.
- Run full test suite nightly.
- Fail fast → stop pipeline if core tests fail.
Example GitHub Actions snippet:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci
- run: npm test -- --coverage