|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from codeflash.languages.javascript.vitest_runner import _ensure_codeflash_vitest_config |
| 6 | + |
| 7 | + |
| 8 | +def test_codeflash_vitest_config_overrides_setupfiles(tmp_path: Path) -> None: |
| 9 | + project_root = tmp_path.resolve() |
| 10 | + |
| 11 | + # Create a project with setup file |
| 12 | + (project_root / "test").mkdir() |
| 13 | + (project_root / "test" / "setup.ts").write_text("// Setup file\n", encoding="utf-8") |
| 14 | + |
| 15 | + vitest_config = """import { defineConfig } from 'vitest/config'; |
| 16 | +
|
| 17 | +export default defineConfig({ |
| 18 | + test: { |
| 19 | + setupFiles: ["test/setup.ts"], // Relative path - will cause issues |
| 20 | + include: ["src/**/*.test.ts"], |
| 21 | + }, |
| 22 | +}); |
| 23 | +""" |
| 24 | + (project_root / "vitest.config.ts").write_text(vitest_config, encoding="utf-8") |
| 25 | + |
| 26 | + codeflash_config_path = _ensure_codeflash_vitest_config(project_root) |
| 27 | + |
| 28 | + assert codeflash_config_path is not None |
| 29 | + assert codeflash_config_path.exists() |
| 30 | + |
| 31 | + config_content = codeflash_config_path.read_text(encoding="utf-8") |
| 32 | + |
| 33 | + assert "setupFiles" in config_content, ( |
| 34 | + "Generated config must explicitly handle setupFiles to prevent " |
| 35 | + "relative path resolution issues. Current config:\n" + config_content |
| 36 | + ) |
| 37 | + assert "setupFiles: []" in config_content or "setupFiles:" in config_content, ( |
| 38 | + "setupFiles must be explicitly set in the merged config" |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_codeflash_vitest_config_without_setupfiles(tmp_path: Path) -> None: |
| 43 | + project_root = tmp_path.resolve() |
| 44 | + |
| 45 | + vitest_config = """import { defineConfig } from 'vitest/config'; |
| 46 | +
|
| 47 | +export default defineConfig({ |
| 48 | + test: { |
| 49 | + include: ["src/**/*.test.ts"], |
| 50 | + }, |
| 51 | +}); |
| 52 | +""" |
| 53 | + (project_root / "vitest.config.ts").write_text(vitest_config, encoding="utf-8") |
| 54 | + |
| 55 | + codeflash_config_path = _ensure_codeflash_vitest_config(project_root) |
| 56 | + |
| 57 | + assert codeflash_config_path is not None |
| 58 | + assert codeflash_config_path.exists() |
| 59 | + |
| 60 | + config_content = codeflash_config_path.read_text(encoding="utf-8") |
| 61 | + assert "mergeConfig" in config_content or "defineConfig" in config_content |
0 commit comments