Skip to content

Fix Vitest coverage collection by explicitly enabling coverage in config and CLI#1989

Closed
mohammedahmed18 wants to merge 2 commits intomainfrom
fix/vitest-coverage-enabled
Closed

Fix Vitest coverage collection by explicitly enabling coverage in config and CLI#1989
mohammedahmed18 wants to merge 2 commits intomainfrom
fix/vitest-coverage-enabled

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Issue

Vitest coverage was not being collected (0% coverage) despite proper configuration. 15 out of 20 optimization runs showed this issue.

Trace IDs: 07be59c3-e53c-4350-b874-9d1fee5238d1 and 14 others

Symptoms:

  • Coverage provider @vitest/coverage-v8 installed
  • Coverage flags passed: --coverage, --coverage.reporter=json, --coverage.reportsDirectory
  • Coverage directory never created by Vitest
  • Log message: "Coverage directory does not exist"

Root Cause

Vitest 4's mergeConfig() doesn't properly enable coverage when project configs have complex coverage settings. The project config had:

coverage: {
  provider: "v8",
  reporter: ["text", "lcov"],
  include: ["./src/**/*.ts"],
}

Without explicit enabled: true in the config override, Vitest would not collect coverage even with --coverage flag on command line.

Fix

File: codeflash/languages/javascript/vitest_runner.py

  1. Config Override (lines 268, 291): Added coverage.enabled: true to generated codeflash.vitest.config.mjs
  2. Command Line (line 486): Re-added --coverage.enabled=true flag (was removed as "redundant")
  3. Include Pattern: Added broad coverage.include pattern to override restrictive project patterns

Setting enabled: true in BOTH config and CLI ensures coverage is reliably collected regardless of project configuration structure.

Testing

  • Created tests/languages/javascript/test_vitest_coverage_enabled.py with 2 test cases
  • All 46 existing vitest tests pass
  • uv run prek passes with no linting/type errors

Impact

Fixes coverage collection for 15/20 failed optimization runs in openclaw project.

…fig and CLI

## Issue
Vitest coverage was not being collected (0% coverage) despite:
- Coverage provider (@vitest/coverage-v8) being installed
- All coverage flags being passed (--coverage, --coverage.reporter=json, etc.)
- Coverage directory never created

Affected 15 out of 20 optimization runs.

Trace IDs: 07be59c3-e53c-4350-b874-9d1fee5238d1 and 14 others

## Root Cause
Vitest 4's mergeConfig() doesn't properly enable coverage when project configs
have complex coverage settings. The project config had:
- coverage.provider: "v8"
- coverage.reporter: ["text", "lcov"]
- coverage.include: ["./src/**/*.ts"]

Without explicit `enabled: true` in the config override, Vitest would not
collect coverage even with --coverage flag on command line.

## Fix
1. Added `coverage.enabled: true` to generated config override (both mergeConfig
   and standalone cases)
2. Re-added `--coverage.enabled=true` command-line flag (was previously removed
   as "redundant")
3. Added broad `coverage.include` pattern to override restrictive project patterns

Setting enabled: true in BOTH config and CLI ensures coverage is reliably
collected regardless of how project configs are structured.

## Testing
- Added test_vitest_coverage_enabled.py with 2 test cases
- All existing vitest tests pass (46 tests)
- uv run prek passes with no linting/type errors

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 3, 2026

Claude finished @mohammedahmed18's task in 2m 8s —— View job


PR Review Summary

  • Triage PR size — SMALL (49 lines of production code changed)
  • Run lint/typecheck
  • Resolve stale review threads — none found
  • Review code changes
  • Duplicate detection
  • Post summary

Prek Checks

Auto-fixed 2 Q000 (bad-quotes-inline-string) errors and reformatted vitest_runner.py. Committed as style: auto-fix ruff linting issues. Prek now passes cleanly.

Code Review

Bug: import re inside try block

vitest_runner.py:178import re is placed inside the try block rather than at the module level. While there's a pre-existing instance at line ~742, this pattern isn't ideal. re is a stdlib module and should be imported at the top of the file. Fix this →

Testing convention violations in test_vitest_coverage_enabled.py

Per .claude/rules/testing.md, new tests must:

  1. Use tmp_path pytest fixture instead of tempfile.TemporaryDirectory() (lines 21, 71)
  2. Call .resolve() on Path objects before passing to functions under test (lines 22, 72)

The pytest import on line 14 is also unused and should be removed. Fix this →

Bundled unrelated changes

This PR bundles 3 fixes with no separation:

  • Coverage enabled: true fix (stated purpose)
  • setupFiles: [] override (mentioned only in comments, not in PR description)
  • _is_vitest_workspace regex improvement to avoid false positives on comments

The last two are meaningful standalone fixes that deserve separate description/tracking. Not a blocking issue, but worth noting for traceability.

coverage.include override may be too broad

Lines 272/294: The include pattern ['**/*.ts', '**/*.js', ...] will include test files and generated files in coverage reports. The project's original config had ['./src/**/*.ts'] specifically to exclude non-source files. This could produce noisy/misleading coverage numbers by including test code in coverage metrics. A pattern like ['**/src/**/*.{ts,js,tsx,jsx,mjs,mts}'] or at least excluding **/*.test.* would be more precise.

Duplicate Detection

No duplicates detected. All changes are confined to vitest_runner.py with no equivalent logic in sibling language modules.


Last updated: 2026-04-03T22:34 UTC

Co-authored-by: mohammedahmed18 <undefined@users.noreply.github.com>
@mohammedahmed18
Copy link
Copy Markdown
Contributor Author

deuplicated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant