88
99
1010class TestJestRootsConfiguration :
11- """Tests for Jest --roots flag handling ."""
11+ """Tests for Jest runtime config creation when test files are outside the project root ."""
1212
13- def test_behavioral_tests_adds_roots_for_test_directories (self ):
14- """Test that run_jest_behavioral_tests adds --roots for test directories ."""
15- from codeflash .languages .javascript .test_runner import run_jest_behavioral_tests
13+ def test_no_runtime_config_when_tests_inside_project_root (self ):
14+ """Test that no runtime config is created when test files are inside the project root ."""
15+ from codeflash .languages .javascript .test_runner import clear_created_config_files , get_created_config_files , run_jest_behavioral_tests
1616 from codeflash .models .models import TestFile , TestFiles
1717 from codeflash .models .test_type import TestType
1818
19- # Create mock test files in a test directory
2019 with tempfile .TemporaryDirectory () as tmpdir :
2120 tmpdir_path = Path (tmpdir ).resolve ()
2221 test_dir = tmpdir_path / "test"
2322 test_dir .mkdir ()
2423
25- # Create package.json to simulate a Node project
2624 (tmpdir_path / "package.json" ).write_text ('{"name": "test"}' )
2725
28- # Create mock test files
2926 test_file1 = test_dir / "test_func__unit_test_0.test.ts"
30- test_file2 = test_dir / "test_func__unit_test_1.test.ts"
3127 test_file1 .write_text ("// test 1" )
32- test_file2 .write_text ("// test 2" )
3328
3429 mock_test_files = TestFiles (
3530 test_files = [
@@ -39,16 +34,11 @@ def test_behavioral_tests_adds_roots_for_test_directories(self):
3934 benchmarking_file_path = test_file1 ,
4035 test_type = TestType .GENERATED_REGRESSION ,
4136 ),
42- TestFile (
43- original_file_path = test_file2 ,
44- instrumented_behavior_file_path = test_file2 ,
45- benchmarking_file_path = test_file2 ,
46- test_type = TestType .GENERATED_REGRESSION ,
47- ),
4837 ]
4938 )
5039
51- # Mock subprocess.run to capture the command
40+ clear_created_config_files ()
41+
5242 with patch ("subprocess.run" ) as mock_run :
5343 mock_result = MagicMock ()
5444 mock_result .stdout = ""
@@ -64,40 +54,32 @@ def test_behavioral_tests_adds_roots_for_test_directories(self):
6454 project_root = tmpdir_path ,
6555 )
6656 except Exception :
67- pass # Expected to fail since no real Jest
57+ pass
6858
69- # Verify the command included --roots
7059 if mock_run .called :
71- call_args = mock_run .call_args
72- cmd = call_args [0 ][0 ]
73-
74- # Find --roots flags in the command
75- roots_flags = []
76- for i , arg in enumerate (cmd ):
77- if arg == "--roots" and i + 1 < len (cmd ):
78- roots_flags .append (cmd [i + 1 ])
79-
80- # Should have added the test directory as a root
81- assert len (roots_flags ) > 0 , "Expected --roots flag in Jest command"
82- assert str (test_dir ) in roots_flags or any (
83- str (test_dir ) in root for root in roots_flags
84- ), f"Expected test directory { test_dir } in --roots flags: { roots_flags } "
85-
86- def test_benchmarking_tests_adds_roots_for_test_directories (self ):
87- """Test that run_jest_benchmarking_tests adds --roots for test directories."""
88- from codeflash .languages .javascript .test_runner import run_jest_benchmarking_tests
60+ cmd = mock_run .call_args [0 ][0 ]
61+ # No --roots flags should be present
62+ assert "--roots" not in cmd , "Should not have --roots flags when tests are inside project root"
63+ # No runtime config should have been created
64+ runtime_configs = [f for f in get_created_config_files () if "codeflash.runtime" in f .name ]
65+ assert len (runtime_configs ) == 0 , "Should not create runtime config when tests are inside project root"
66+
67+ clear_created_config_files ()
68+
69+ def test_behavioral_tests_creates_runtime_config_for_external_tests (self ):
70+ """Test that run_jest_behavioral_tests creates a runtime config when tests are outside the project root."""
71+ from codeflash .languages .javascript .test_runner import clear_created_config_files , get_created_config_files , run_jest_behavioral_tests
8972 from codeflash .models .models import TestFile , TestFiles
9073 from codeflash .models .test_type import TestType
9174
92- with tempfile .TemporaryDirectory () as tmpdir :
93- tmpdir_path = Path (tmpdir ).resolve ()
94- test_dir = tmpdir_path / "test"
95- test_dir .mkdir ()
75+ with tempfile .TemporaryDirectory () as project_dir , tempfile .TemporaryDirectory () as external_dir :
76+ project_path = Path (project_dir ).resolve ()
77+ external_path = Path (external_dir ).resolve ()
9678
97- (tmpdir_path / "package.json" ).write_text ('{"name": "test"}' )
79+ (project_path / "package.json" ).write_text ('{"name": "test"}' )
9880
99- test_file = test_dir / "test_func__perf_test_0 .test.ts"
100- test_file .write_text ("// perf test" )
81+ test_file = external_path / "test_func__unit_test_0 .test.ts"
82+ test_file .write_text ("// test 1 " )
10183
10284 mock_test_files = TestFiles (
10385 test_files = [
@@ -110,6 +92,8 @@ def test_benchmarking_tests_adds_roots_for_test_directories(self):
11092 ]
11193 )
11294
95+ clear_created_config_files ()
96+
11397 with patch ("subprocess.run" ) as mock_run :
11498 mock_result = MagicMock ()
11599 mock_result .stdout = ""
@@ -118,41 +102,43 @@ def test_benchmarking_tests_adds_roots_for_test_directories(self):
118102 mock_run .return_value = mock_result
119103
120104 try :
121- run_jest_benchmarking_tests (
105+ run_jest_behavioral_tests (
122106 test_paths = mock_test_files ,
123107 test_env = {},
124- cwd = tmpdir_path ,
125- project_root = tmpdir_path ,
108+ cwd = project_path ,
109+ project_root = project_path ,
126110 )
127111 except Exception :
128112 pass
129113
130114 if mock_run .called :
131- call_args = mock_run .call_args
132- cmd = call_args [0 ][0 ]
115+ cmd = mock_run .call_args [0 ][0 ]
116+ config_args = [arg for arg in cmd if arg .startswith ("--config=" )]
117+ assert any ("codeflash.runtime" in arg for arg in config_args ), (
118+ f"Expected runtime config in --config flag, got: { config_args } "
119+ )
133120
134- roots_flags = [ ]
135- for i , arg in enumerate ( cmd ):
136- if arg == "--roots" and i + 1 < len ( cmd ):
137- roots_flags . append ( cmd [ i + 1 ])
121+ runtime_configs = [ f for f in get_created_config_files () if "codeflash.runtime" in f . name ]
122+ assert len ( runtime_configs ) == 1 , f"Expected 1 runtime config, got { len ( runtime_configs ) } "
123+ config_content = runtime_configs [ 0 ]. read_text ( encoding = "utf-8" )
124+ assert str ( external_path ) in config_content , "Runtime config should contain external test directory"
138125
139- assert len ( roots_flags ) > 0 , "Expected --roots flag in Jest command"
126+ clear_created_config_files ()
140127
141- def test_line_profile_tests_adds_roots_for_test_directories (self ):
142- """Test that run_jest_line_profile_tests adds --roots for test directories ."""
143- from codeflash .languages .javascript .test_runner import run_jest_line_profile_tests
128+ def test_benchmarking_tests_creates_runtime_config_for_external_tests (self ):
129+ """Test that run_jest_benchmarking_tests creates a runtime config when tests are outside the project root ."""
130+ from codeflash .languages .javascript .test_runner import clear_created_config_files , get_created_config_files , run_jest_benchmarking_tests
144131 from codeflash .models .models import TestFile , TestFiles
145132 from codeflash .models .test_type import TestType
146133
147- with tempfile .TemporaryDirectory () as tmpdir :
148- tmpdir_path = Path (tmpdir )
149- test_dir = tmpdir_path / "test"
150- test_dir .mkdir ()
134+ with tempfile .TemporaryDirectory () as project_dir , tempfile .TemporaryDirectory () as external_dir :
135+ project_path = Path (project_dir ).resolve ()
136+ external_path = Path (external_dir ).resolve ()
151137
152- (tmpdir_path / "package.json" ).write_text ('{"name": "test"}' )
138+ (project_path / "package.json" ).write_text ('{"name": "test"}' )
153139
154- test_file = test_dir / "test_func__line_profile .test.ts"
155- test_file .write_text ("// line profile test" )
140+ test_file = external_path / "test_func__perf_test_0 .test.ts"
141+ test_file .write_text ("// perf test" )
156142
157143 mock_test_files = TestFiles (
158144 test_files = [
@@ -165,6 +151,8 @@ def test_line_profile_tests_adds_roots_for_test_directories(self):
165151 ]
166152 )
167153
154+ clear_created_config_files ()
155+
168156 with patch ("subprocess.run" ) as mock_run :
169157 mock_result = MagicMock ()
170158 mock_result .stdout = ""
@@ -173,63 +161,48 @@ def test_line_profile_tests_adds_roots_for_test_directories(self):
173161 mock_run .return_value = mock_result
174162
175163 try :
176- run_jest_line_profile_tests (
164+ run_jest_benchmarking_tests (
177165 test_paths = mock_test_files ,
178166 test_env = {},
179- cwd = tmpdir_path ,
180- project_root = tmpdir_path ,
167+ cwd = project_path ,
168+ project_root = project_path ,
181169 )
182170 except Exception :
183171 pass
184172
185- if mock_run .called :
186- call_args = mock_run .call_args
187- cmd = call_args [0 ][0 ]
188-
189- roots_flags = []
190- for i , arg in enumerate (cmd ):
191- if arg == "--roots" and i + 1 < len (cmd ):
192- roots_flags .append (cmd [i + 1 ])
173+ runtime_configs = [f for f in get_created_config_files () if "codeflash.runtime" in f .name ]
174+ assert len (runtime_configs ) == 1 , "Expected runtime config for external test files"
193175
194- assert len ( roots_flags ) > 0 , "Expected --roots flag in Jest command"
176+ clear_created_config_files ()
195177
196- def test_multiple_test_directories_all_added_to_roots (self ):
197- """Test that multiple test directories are all added as --roots ."""
198- from codeflash .languages .javascript .test_runner import run_jest_behavioral_tests
178+ def test_line_profile_tests_creates_runtime_config_for_external_tests (self ):
179+ """Test that run_jest_line_profile_tests creates a runtime config when tests are outside the project root ."""
180+ from codeflash .languages .javascript .test_runner import clear_created_config_files , get_created_config_files , run_jest_line_profile_tests
199181 from codeflash .models .models import TestFile , TestFiles
200182 from codeflash .models .test_type import TestType
201183
202- with tempfile .TemporaryDirectory () as tmpdir :
203- tmpdir_path = Path (tmpdir )
204- test_dir1 = tmpdir_path / "test"
205- test_dir2 = tmpdir_path / "spec"
206- test_dir1 .mkdir ()
207- test_dir2 .mkdir ()
184+ with tempfile .TemporaryDirectory () as project_dir , tempfile .TemporaryDirectory () as external_dir :
185+ project_path = Path (project_dir ).resolve ()
186+ external_path = Path (external_dir ).resolve ()
208187
209- (tmpdir_path / "package.json" ).write_text ('{"name": "test"}' )
188+ (project_path / "package.json" ).write_text ('{"name": "test"}' )
210189
211- test_file1 = test_dir1 / "test_func__unit_test_0.test.ts"
212- test_file2 = test_dir2 / "test_func__unit_test_1.test.ts"
213- test_file1 .write_text ("// test 1" )
214- test_file2 .write_text ("// test 2" )
190+ test_file = external_path / "test_func__line_profile.test.ts"
191+ test_file .write_text ("// line profile test" )
215192
216193 mock_test_files = TestFiles (
217194 test_files = [
218195 TestFile (
219- original_file_path = test_file1 ,
220- instrumented_behavior_file_path = test_file1 ,
221- benchmarking_file_path = test_file1 ,
222- test_type = TestType .GENERATED_REGRESSION ,
223- ),
224- TestFile (
225- original_file_path = test_file2 ,
226- instrumented_behavior_file_path = test_file2 ,
227- benchmarking_file_path = test_file2 ,
196+ original_file_path = test_file ,
197+ instrumented_behavior_file_path = test_file ,
198+ benchmarking_file_path = test_file ,
228199 test_type = TestType .GENERATED_REGRESSION ,
229200 ),
230201 ]
231202 )
232203
204+ clear_created_config_files ()
205+
233206 with patch ("subprocess.run" ) as mock_run :
234207 mock_result = MagicMock ()
235208 mock_result .stdout = ""
@@ -238,26 +211,19 @@ def test_multiple_test_directories_all_added_to_roots(self):
238211 mock_run .return_value = mock_result
239212
240213 try :
241- run_jest_behavioral_tests (
214+ run_jest_line_profile_tests (
242215 test_paths = mock_test_files ,
243216 test_env = {},
244- cwd = tmpdir_path ,
245- project_root = tmpdir_path ,
217+ cwd = project_path ,
218+ project_root = project_path ,
246219 )
247220 except Exception :
248221 pass
249222
250- if mock_run .called :
251- call_args = mock_run .call_args
252- cmd = call_args [0 ][0 ]
253-
254- roots_flags = []
255- for i , arg in enumerate (cmd ):
256- if arg == "--roots" and i + 1 < len (cmd ):
257- roots_flags .append (cmd [i + 1 ])
223+ runtime_configs = [f for f in get_created_config_files () if "codeflash.runtime" in f .name ]
224+ assert len (runtime_configs ) == 1 , "Expected runtime config for external test files"
258225
259- # Should have two --roots flags (one for each directory)
260- assert len (roots_flags ) == 2 , f"Expected 2 --roots flags, got { len (roots_flags )} "
226+ clear_created_config_files ()
261227
262228
263229class TestVitestTimeoutConfiguration :
0 commit comments