1- """Test that Codeflash Vitest config properly handles setupFiles from project config.
2-
3- This test verifies that when creating a custom Vitest config, setupFiles paths
4- are converted to absolute paths or cleared to prevent resolution issues in nested directories.
5- """
6-
71from pathlib import Path
8- import tempfile
9- import pytest
10-
112
12- def test_codeflash_vitest_config_overrides_setupfiles ():
13- """Test that generated config overrides setupFiles to prevent path resolution issues.
3+ import pytest
144
15- When a project has setupFiles with relative paths, and Codeflash generates tests
16- for functions in nested directories, those relative paths will resolve incorrectly.
5+ from codeflash .languages .javascript .vitest_runner import _ensure_codeflash_vitest_config
176
18- The fix: Convert setupFiles paths to absolute, or disable them for generated tests.
19- """
20- from codeflash .languages .javascript .vitest_runner import _ensure_codeflash_vitest_config
217
22- with tempfile . TemporaryDirectory () as tmpdir :
23- project_root = Path ( tmpdir )
8+ def test_codeflash_vitest_config_overrides_setupfiles ( tmp_path : Path ) -> None :
9+ project_root = tmp_path . resolve ( )
2410
25- # Create a project with setup file
26- (project_root / "test" ).mkdir ()
27- setup_file = project_root / "test" / "setup.ts"
28- setup_file .write_text ("// Setup file\n " )
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" )
2914
30- # Create vitest config with relative setupFiles path
31- vitest_config = """import { defineConfig } from 'vitest/config';
15+ vitest_config = """import { defineConfig } from 'vitest/config';
3216
3317export default defineConfig({
3418 test: {
@@ -37,61 +21,41 @@ def test_codeflash_vitest_config_overrides_setupfiles():
3721 },
3822});
3923"""
40- (project_root / "vitest.config.ts" ).write_text (vitest_config )
41-
42- # Call the function to create Codeflash config
43- codeflash_config_path = _ensure_codeflash_vitest_config (project_root )
44-
45- # Verify the config was created
46- assert codeflash_config_path is not None
47- assert codeflash_config_path .exists ()
48-
49- # Read the generated config
50- config_content = codeflash_config_path .read_text ()
24+ (project_root / "vitest.config.ts" ).write_text (vitest_config , encoding = "utf-8" )
5125
52- # The config should either:
53- # 1. Set setupFiles to an empty array (disable setup files for generated tests)
54- # 2. OR convert the path to absolute using project root resolution
26+ codeflash_config_path = _ensure_codeflash_vitest_config (project_root )
5527
56- # Check that setupFiles is mentioned and handled in the merge
57- assert "setupFiles" in config_content , (
58- "Generated config must explicitly handle setupFiles to prevent "
59- "relative path resolution issues. Current config:\n " + config_content
60- )
28+ assert codeflash_config_path is not None
29+ assert codeflash_config_path .exists ()
6130
62- # The config should set setupFiles to [] or to absolute paths
63- # This prevents the relative path from being resolved incorrectly
64- assert ("setupFiles: []" in config_content or
65- "setupFiles:" in config_content ), (
66- "setupFiles must be explicitly set in the merged config"
67- )
31+ config_content = codeflash_config_path .read_text (encoding = "utf-8" )
6832
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+ )
6940
70- def test_codeflash_vitest_config_without_setupfiles ():
71- """Test that configs without setupFiles still work correctly."""
72- from codeflash .languages .javascript .vitest_runner import _ensure_codeflash_vitest_config
7341
74- with tempfile . TemporaryDirectory () as tmpdir :
75- project_root = Path ( tmpdir )
42+ def test_codeflash_vitest_config_without_setupfiles ( tmp_path : Path ) -> None :
43+ project_root = tmp_path . resolve ( )
7644
77- # Create vitest config WITHOUT setupFiles
78- vitest_config = """import { defineConfig } from 'vitest/config';
45+ vitest_config = """import { defineConfig } from 'vitest/config';
7946
8047export default defineConfig({
8148 test: {
8249 include: ["src/**/*.test.ts"],
8350 },
8451});
8552"""
86- (project_root / "vitest.config.ts" ).write_text (vitest_config )
53+ (project_root / "vitest.config.ts" ).write_text (vitest_config , encoding = "utf-8" )
8754
88- # Call the function to create Codeflash config
89- codeflash_config_path = _ensure_codeflash_vitest_config (project_root )
55+ codeflash_config_path = _ensure_codeflash_vitest_config (project_root )
9056
91- # Verify the config was created
92- assert codeflash_config_path is not None
93- assert codeflash_config_path .exists ()
57+ assert codeflash_config_path is not None
58+ assert codeflash_config_path .exists ()
9459
95- # Config should be created successfully
96- config_content = codeflash_config_path .read_text ()
97- assert "mergeConfig" in config_content or "defineConfig" in config_content
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