Skip to content

Commit 2124926

Browse files
authored
Merge pull request #1988 from codeflash-ai/fix/vitest-coverage-override
Fix Vitest coverage collection by overriding coverage.reporter
2 parents 81be416 + d8c2b94 commit 2124926

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

codeflash/languages/javascript/vitest_runner.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ def _ensure_codeflash_vitest_config(project_root: Path) -> Path | None:
259259
// incorrectly when tests are in subdirectories (e.g., extensions/discord/test/).
260260
// Codeflash-generated tests are self-contained and don't require project setup files.
261261
setupFiles: [],
262+
// Override coverage settings to ensure JSON reporter is used.
263+
// Vitest's mergeConfig doesn't properly handle nested coverage object merge with
264+
// command-line flags, so we explicitly set reporter here to guarantee coverage
265+
// files are written to the expected location (coverage-final.json).
266+
coverage: {{
267+
reporter: ['json'],
268+
}},
262269
}},
263270
}});
264271
"""
@@ -275,6 +282,10 @@ def _ensure_codeflash_vitest_config(project_root: Path) -> Path | None:
275282
exclude: ['**/node_modules/**', '**/dist/**'],
276283
// Use forks pool so timing markers from process.stdout.write flow to parent stdout
277284
pool: 'forks',
285+
// Override coverage settings to ensure JSON reporter is used
286+
coverage: {
287+
reporter: ['json'],
288+
},
278289
},
279290
});
280291
"""
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""Test that Codeflash Vitest config properly overrides coverage settings."""
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from codeflash.languages.javascript.vitest_runner import _ensure_codeflash_vitest_config
8+
9+
10+
def test_codeflash_vitest_config_overrides_coverage(tmp_path: Path) -> None:
11+
project_root = tmp_path.resolve()
12+
13+
vitest_config = project_root / "vitest.config.ts"
14+
vitest_config.write_text(
15+
"""
16+
import { defineConfig } from 'vitest/config';
17+
18+
export default defineConfig({
19+
test: {
20+
include: ['test/**/*.test.ts'],
21+
coverage: {
22+
provider: 'v8',
23+
reporter: ['text', 'lcov'],
24+
all: false,
25+
thresholds: {
26+
lines: 70,
27+
functions: 70,
28+
},
29+
},
30+
},
31+
});
32+
""",
33+
encoding="utf-8",
34+
)
35+
36+
config_path = _ensure_codeflash_vitest_config(project_root)
37+
38+
assert config_path is not None, "Config should be created"
39+
assert config_path.exists(), "Config file should exist"
40+
41+
config_content = config_path.read_text(encoding="utf-8")
42+
43+
assert "mergeConfig" in config_content, "Should use mergeConfig"
44+
assert "import originalConfig from './vitest.config.ts'" in config_content
45+
assert "coverage:" in config_content, (
46+
"Config must explicitly override coverage settings to ensure "
47+
"json reporter is used regardless of project config"
48+
)
49+
assert "reporter:" in config_content, "Config must override coverage.reporter to ['json']"
50+
assert "['json']" in config_content or '["json"]' in config_content, (
51+
"Coverage reporter must be set to ['json'] to ensure coverage files are written in the expected format"
52+
)
53+
54+
55+
def test_codeflash_vitest_config_without_original_coverage(tmp_path: Path) -> None:
56+
project_root = tmp_path.resolve()
57+
58+
vitest_config = project_root / "vitest.config.ts"
59+
vitest_config.write_text(
60+
"""
61+
import { defineConfig } from 'vitest/config';
62+
63+
export default defineConfig({
64+
test: {
65+
include: ['test/**/*.test.ts'],
66+
},
67+
});
68+
""",
69+
encoding="utf-8",
70+
)
71+
72+
config_path = _ensure_codeflash_vitest_config(project_root)
73+
74+
assert config_path is not None
75+
assert config_path.exists()
76+
77+
config_content = config_path.read_text(encoding="utf-8")
78+
79+
assert "coverage:" in config_content, "Config must explicitly set coverage even when original doesn't have it"

0 commit comments

Comments
 (0)