|
| 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