Skip to content

feat: Add code coverage threshold enforcement in vitest.config.ts and CI badge #288

@d-oit

Description

@d-oit

Problem

vitest.config.ts runs coverage with @vitest/coverage-v8, but there are no coverage thresholds configured. The CI workflow (ci-and-labels.yml) does not fail on low coverage, meaning regressions can silently reduce test coverage. There is no coverage badge in the README.

Current State

// vitest.config.ts - coverage section (approximate)
coverage: {
  provider: 'v8',
  reporter: ['text', 'html', 'json'],
  // No thresholds configured!
}
# CI workflow - no coverage threshold step
- run: npm run test:coverage
# Missing: fail if below threshold, upload to Codecov

Proposed Changes

1. Add coverage thresholds to vitest.config.ts

export default defineConfig({
  test: {
    coverage: {
      provider: 'v8',
      reporter: ['text', 'html', 'json', 'lcov'],
      include: ['src/**/*.{ts,tsx}'],
      exclude: [
        'src/**/*.d.ts',
        'src/**/*.stories.{ts,tsx}',
        'src/vite-env.d.ts',
        'src/main.tsx',
      ],
      thresholds: {
        lines: 60,
        branches: 50,
        functions: 60,
        statements: 60,
        // Per-file thresholds to prevent new files from having 0% coverage
        perFile: true,
        autoUpdate: false,
      },
    },
  },
});

2. Update CI workflow

# .github/workflows/ci-and-labels.yml
- name: Run tests with coverage
  run: npm run test:coverage
  
- name: Upload coverage to Codecov
  uses: codecov/codecov-action@v4
  with:
    token: ${{ secrets.CODECOV_TOKEN }}
    files: ./coverage/lcov.info
    flags: unittests
    name: do-knowledge-studio
    fail_ci_if_error: false

3. Add test:coverage script to package.json

"scripts": {
  "test": "vitest run",
  "test:coverage": "vitest run --coverage",
  "test:coverage:watch": "vitest --coverage"
}

4. Add coverage badge to README

<!-- README.md -->
[![codecov](https://codecov.io/gh/d-oit/do-knowledge-studio/branch/main/graph/badge.svg)](https://codecov.io/gh/d-oit/do-knowledge-studio)
[![Tests](https://github.com/d-oit/do-knowledge-studio/actions/workflows/ci-and-labels.yml/badge.svg)](https://github.com/d-oit/do-knowledge-studio/actions/workflows/ci-and-labels.yml)

5. Add .codecov.yml configuration

# .codecov.yml
coverage:
  status:
    project:
      default:
        target: 60%
        threshold: 5%
    patch:
      default:
        target: 50%
        threshold: 10%
comment:
  layout: "reach, diff, flags, files"
  behavior: default
  require_changes: false

Acceptance Criteria

  • vitest.config.ts has coverage thresholds: lines 60%, branches 50%, functions 60%
  • lcov reporter added for Codecov compatibility
  • test:coverage script in package.json
  • CI workflow has coverage upload step with codecov/codecov-action@v4
  • CI fails if coverage drops below configured thresholds
  • Coverage badge added to README.md
  • .codecov.yml with project/patch coverage targets
  • Threshold values documented in CONTRIBUTING.md

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions