Skip to content

Commit 89c7b2b

Browse files
committed
fix: add --roots flag to Jest to include test file directories
Some projects configure Jest with restricted roots (e.g., roots: ["<rootDir>/src"]) which prevents Jest from finding tests written to other directories like "test/". Even with --runTestsByPath, Jest validates that files exist within the configured roots. This fix adds the --roots flag to explicitly include the directories containing the test files being run, ensuring Jest can find and execute them regardless of the project's roots configuration. Fixes issue where "No tests found" error occurs on projects with custom Jest roots configuration.
1 parent e7e562e commit 89c7b2b

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

codeflash/languages/javascript/test_runner.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,14 @@ def run_jest_behavioral_tests(
257257

258258
if test_files:
259259
jest_cmd.append("--runTestsByPath")
260-
jest_cmd.extend(str(Path(f).resolve()) for f in test_files)
260+
resolved_test_files = [str(Path(f).resolve()) for f in test_files]
261+
jest_cmd.extend(resolved_test_files)
262+
# Add --roots to include directories containing test files
263+
# This is needed because some projects configure Jest with restricted roots
264+
# (e.g., roots: ["<rootDir>/src"]) which excludes the test directory
265+
test_dirs = {str(Path(f).resolve().parent) for f in test_files}
266+
for test_dir in sorted(test_dirs):
267+
jest_cmd.extend(["--roots", test_dir])
261268

262269
if timeout:
263270
jest_cmd.append(f"--testTimeout={timeout * 1000}") # Jest uses milliseconds
@@ -467,7 +474,12 @@ def run_jest_benchmarking_tests(
467474

468475
if test_files:
469476
jest_cmd.append("--runTestsByPath")
470-
jest_cmd.extend(str(Path(f).resolve()) for f in test_files)
477+
resolved_test_files = [str(Path(f).resolve()) for f in test_files]
478+
jest_cmd.extend(resolved_test_files)
479+
# Add --roots to include directories containing test files
480+
test_dirs = {str(Path(f).resolve().parent) for f in test_files}
481+
for test_dir in sorted(test_dirs):
482+
jest_cmd.extend(["--roots", test_dir])
471483

472484
if timeout:
473485
jest_cmd.append(f"--testTimeout={timeout * 1000}")
@@ -596,7 +608,12 @@ def run_jest_line_profile_tests(
596608

597609
if test_files:
598610
jest_cmd.append("--runTestsByPath")
599-
jest_cmd.extend(str(Path(f).resolve()) for f in test_files)
611+
resolved_test_files = [str(Path(f).resolve()) for f in test_files]
612+
jest_cmd.extend(resolved_test_files)
613+
# Add --roots to include directories containing test files
614+
test_dirs = {str(Path(f).resolve().parent) for f in test_files}
615+
for test_dir in sorted(test_dirs):
616+
jest_cmd.extend(["--roots", test_dir])
600617

601618
if timeout:
602619
jest_cmd.append(f"--testTimeout={timeout * 1000}")

0 commit comments

Comments
 (0)