Fix: Use correct Jest config per-package in monorepo optimizations#1993
Fix: Use correct Jest config per-package in monorepo optimizations#1993mohammedahmed18 wants to merge 2 commits intomainfrom
Conversation
In --all mode with monorepos, test_cfg.js_project_root was set once based on the first file and reused for all functions. When optimizing functions from different packages (e.g., worker after server), Jest would run with the wrong package's config, causing "Cannot find module" errors due to incorrect moduleNameMapper resolution. **Root Cause:** optimizer.py:531 calls setup_test_config() once, setting js_project_root. This was then passed to run_jest_behavioral_tests() for ALL functions, even those in different monorepo packages. **Fix:** In run_jest_behavioral_tests(), run_jest_benchmarking_tests(), and run_jest_line_profile_tests(), detect the correct package root from the test file location when the provided project_root points to a different peer package. **Impact:** - Affected 25/46 optimization runs (~54%) with "Cannot find module" errors - Trace IDs: 02f0351a-db89-4ebc-a2e6-c45b19061152 (and 24 others) - Target: budibase monorepo (packages/server vs packages/worker) **Testing:** - Added regression test: test_monorepo_project_root_bug.py - All 316 JavaScript tests pass - Verified external test files still work correctly Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Claude finished @mohammedahmed18's task in 3m 19s —— View job PR Review Summary
Prek ChecksThree multi-line Mypy: 7 pre-existing errors in Code ReviewThe core fix is correct. The root cause (stale Issues found: 1. Undocumented second fix (line 389) if base_config_path and base_config_path.suffix != ".ts":This is a separate fix for TypeScript Jest configs ( 2. Code duplication — same block repeated 3× (medium) The monorepo detection block is copy-pasted identically into def _resolve_monorepo_project_root(test_files: list[str], project_root: Path | None) -> Path | None:
if test_files and project_root:
detected = find_node_project_root(Path(test_files[0]))
if (detected and detected != project_root
and (detected / "package.json").exists()
and detected.parent == project_root.parent):
return detected
elif project_root is None and test_files:
return find_node_project_root(Path(test_files[0]))
return project_root3. Regression test doesn't exercise the fixed code path (medium)
4. Missing Per server_root = find_node_project_root(server_file) # missing .resolve()5. Peer-packages assumption may be too narrow (low)
Duplicate DetectionNo duplicates detected. The detection block is tripled within the same file (flagged above as code duplication) but not duplicated across modules. Last updated: 2026-04-04 |
Co-authored-by: mohammed ahmed <mohammedahmed18@users.noreply.github.com>
Problem
In
--allmode with monorepos,test_cfg.js_project_rootwas set once based on the first file and reused for all functions. When optimizing functions from different packages (e.g.,workerafterserver), Jest would run with the wrong package's config, causing "Cannot find module" errors due to incorrectmoduleNameMapperresolution.Root Cause
optimizer.py:531callssetup_test_config()once, settingjs_project_rootrun_jest_behavioral_tests()for ALL functionspackages/worker/src/tenants.tsusedpackages/server's Jest configSolution
In
run_jest_behavioral_tests(),run_jest_benchmarking_tests(), andrun_jest_line_profile_tests():project_rootonly when it points to a different peer packageImpact
02f0351a-db89-4ebc-a2e6-c45b19061152and 24 othersTesting
test_monorepo_project_root_bug.pyFiles Changed
codeflash/languages/javascript/test_runner.py- Fix project root detection in 3 locationstests/test_languages/test_monorepo_project_root_bug.py- New regression tests