Fix: Disable Jest globalSetup/globalTeardown to avoid external dependencies#2002
Fix: Disable Jest globalSetup/globalTeardown to avoid external dependencies#2002mohammedahmed18 wants to merge 2 commits intomainfrom
Conversation
**Problem:** Codeflash was failing to run tests for projects that use Jest globalSetup or globalTeardown hooks with external dependencies (e.g., Docker, testcontainers). When these hooks failed, all optimization attempts were blocked. **Root Cause:** Three config generation functions were extending original Jest configs but not disabling globalSetup/globalTeardown hooks, causing inherited dependencies to fail: - _create_codeflash_jest_config - _create_runtime_jest_config - _get_jest_config_for_project (only created configs for bundler moduleResolution) **Solution:** 1. All generated Jest configs now explicitly set: - globalSetup: undefined - globalTeardown: undefined 2. _get_jest_config_for_project now ALWAYS creates a codeflash config (not just for bundler moduleResolution projects) 3. Added check to skip requiring TypeScript configs (jest.config.ts) which cannot be parsed by CommonJS require() **Testing:** - Added 6 new unit tests covering all config generation scenarios - All 69 existing JavaScript tests pass - Verified fix eliminates Docker/globalSetup errors in Budibase optimization runs **Impact:** Codeflash-generated tests are now self-contained and don't trigger project-level global setup/teardown hooks, making optimization work on more projects. Fixes: trace IDs 0c5fcd53-a797-4268-8344-e730f40be867, 76f8e8c6-e988-4db9-a02c-5fe0b065d773, d5759d1d-3ac4-457a-a748-bf9edd15c6b0
|
Claude finished @mohammedahmed18's task in 5m 0s —— View job PR Review Summary
Prek ChecksRuff format reports 1 file reformatted, but this appears to be a pre-existing file in the branch that differs from Fixed and pushed: The new test file had several issues that I've corrected in commit
Code ReviewBug: Duplicate In Behavior change scope: Previously only ESM/bundler projects got a wrapper config; now ALL projects do. This is intentional per the PR description, but has a subtle risk: if No tests for the monorepo root detection logic Fix this → The monorepo root detection block is added to 3 functions but is not tested:
Duplicate DetectionHIGH confidence: Monorepo detection block duplicated 3× Fix this → The same 10-line monorepo detection block appears identically in all three Jest runner functions (lines ~800, ~1071, ~1255). This should be extracted into a helper like Test CoverageThe 6 new tests cover the core fix well (all config generation scenarios for globalSetup/globalTeardown). Coverage gap: no tests for the monorepo root detection logic added to the runner functions. Last updated: 2026-04-06 |
- Replace tempfile.TemporaryDirectory() with pytest tmp_path fixture - Remove unused imports (tempfile, pytest) - Add -> None return type annotations to all test functions - Pass encoding="utf-8" to write_text/read_text calls Co-authored-by: mohammed ahmed <mohammedahmed18@users.noreply.github.com>
Problem
Codeflash optimization runs were failing on projects using Jest with
globalSetuporglobalTeardownhooks that depend on external tools like Docker.Affected projects: Budibase (and any Jest project with global hooks requiring unavailable dependencies)
Symptoms:
Jest: Got error running globalSetuperrorsdocker: not foundblocking all test executionExample trace IDs:
Root Cause
Codeflash generates wrapper Jest configs that extend original configs using:
This caused
globalSetupandglobalTeardownhooks to be inherited, triggering external dependencies.Solution
Modified 3 config generation functions to explicitly disable global hooks:
_create_codeflash_jest_config- Main wrapper configglobalSetup: undefinedandglobalTeardown: undefined_create_runtime_jest_config- Runtime config for external test directories_get_jest_config_for_project- Config resolverTesting
uv run prek)Impact
Codeflash-generated tests are now self-contained and won't trigger project-level setup/teardown hooks. This makes optimization work on more real-world projects without environmental prerequisites.
Review Notes
The fix is conservative - it only affects Codeflash-generated Jest configs, not the project's original configs. User tests continue to use global hooks normally.