Skip to content

Commit c66cc4e

Browse files
misrasaurabh1claude
andcommitted
fix(js): pass project Jest config for TypeScript transformation
When running generated tests on TypeScript projects, Jest was failing with "unexpected token" errors because it wasn't configured to transform TypeScript files. The tests were being run without the project's Jest config file. This fix: - Adds `_find_jest_config()` function to locate Jest config files - Searches project root and parent directories (for monorepo support) - Passes `--config` flag to Jest in all test runner functions - Stops search at git root or monorepo root (package.json with workspaces) Tested against next.js (monorepo) where jest.config.js is at repo root but packages are in nested directories. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5020757 commit c66cc4e

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

codeflash/languages/javascript/test_runner.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,73 @@ def _find_node_project_root(file_path: Path) -> Path | None:
4747
return None
4848

4949

50+
def _find_jest_config(project_root: Path) -> Path | None:
51+
"""Find Jest configuration file in the project.
52+
53+
Searches for common Jest config file names in the project root and parent
54+
directories (for monorepo support). This is important for TypeScript projects
55+
that require specific transformation configurations (e.g., next/jest, ts-jest, babel-jest).
56+
57+
Args:
58+
project_root: Root of the project to search.
59+
60+
Returns:
61+
Path to Jest config file, or None if not found.
62+
63+
"""
64+
# Common Jest config file names, in order of preference
65+
config_names = [
66+
"jest.config.ts",
67+
"jest.config.js",
68+
"jest.config.mjs",
69+
"jest.config.cjs",
70+
"jest.config.json",
71+
]
72+
73+
# First check the project root itself
74+
for config_name in config_names:
75+
config_path = project_root / config_name
76+
if config_path.exists():
77+
logger.debug(f"Found Jest config: {config_path}")
78+
return config_path
79+
80+
# For monorepos, search parent directories up to the filesystem root
81+
# Stop at common monorepo root indicators (git root, package.json with workspaces)
82+
current = project_root.parent
83+
max_depth = 5 # Don't search too far up
84+
depth = 0
85+
86+
while current != current.parent and depth < max_depth:
87+
for config_name in config_names:
88+
config_path = current / config_name
89+
if config_path.exists():
90+
logger.debug(f"Found Jest config in parent directory: {config_path}")
91+
return config_path
92+
93+
# Check if this looks like a monorepo root
94+
package_json = current / "package.json"
95+
if package_json.exists():
96+
try:
97+
import json
98+
99+
with package_json.open("r") as f:
100+
pkg = json.load(f)
101+
if "workspaces" in pkg:
102+
# This is likely the monorepo root, stop here
103+
break
104+
except Exception:
105+
pass
106+
107+
# Check for git root as another stopping point
108+
if (current / ".git").exists():
109+
break
110+
111+
current = current.parent
112+
depth += 1
113+
114+
return None
115+
116+
50117
def _is_esm_project(project_root: Path) -> bool:
51118
"""Check if the project uses ES Modules.
52119
@@ -230,6 +297,11 @@ def run_jest_behavioral_tests(
230297
"--forceExit",
231298
]
232299

300+
# Add Jest config if found - needed for TypeScript transformation
301+
jest_config = _find_jest_config(effective_cwd)
302+
if jest_config:
303+
jest_cmd.append(f"--config={jest_config}")
304+
233305
# Add coverage flags if enabled
234306
if enable_coverage:
235307
jest_cmd.extend(["--coverage", "--coverageReporters=json", f"--coverageDirectory={coverage_dir}"])
@@ -459,6 +531,11 @@ def run_jest_benchmarking_tests(
459531
"--runner=codeflash/loop-runner", # Use custom loop runner for in-process looping
460532
]
461533

534+
# Add Jest config if found - needed for TypeScript transformation
535+
jest_config = _find_jest_config(effective_cwd)
536+
if jest_config:
537+
jest_cmd.append(f"--config={jest_config}")
538+
462539
if test_files:
463540
jest_cmd.append("--runTestsByPath")
464541
resolved_test_files = [str(Path(f).resolve()) for f in test_files]
@@ -593,6 +670,11 @@ def run_jest_line_profile_tests(
593670
"--forceExit",
594671
]
595672

673+
# Add Jest config if found - needed for TypeScript transformation
674+
jest_config = _find_jest_config(effective_cwd)
675+
if jest_config:
676+
jest_cmd.append(f"--config={jest_config}")
677+
596678
if test_files:
597679
jest_cmd.append("--runTestsByPath")
598680
resolved_test_files = [str(Path(f).resolve()) for f in test_files]

0 commit comments

Comments
 (0)