Skip to content

Fix Jest runtime config failing to load TypeScript base configs#1992

Merged
Saga4 merged 1 commit intomainfrom
fix/typescript-jest-config-require
Apr 4, 2026
Merged

Fix Jest runtime config failing to load TypeScript base configs#1992
Saga4 merged 1 commit intomainfrom
fix/typescript-jest-config-require

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

When a project uses jest.config.ts (TypeScript config), the generated runtime config tries to require('./jest.config.ts'), which fails because Node.js CommonJS cannot parse TypeScript syntax without compilation.

Error message:

SyntaxError: Missing initializer in const declaration
    at compileSourceTextModule (node:internal/modules/esm/utils:346:16)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1363:24)

Impact

  • Affected logs: 18 out of 38 optimization runs (~47%)
  • Severity: HIGH - Blocks all TypeScript projects using jest.config.ts
  • Affected trace IDs:
    • 0fd176bf-5c7f-4f41-8396-77c46be86412
    • (and 17 others - see logs in /workspace/logs)

Root Cause

Line 386 in codeflash/languages/javascript/test_runner.py used base_config_path.name directly without checking the file extension. The generated runtime config is always a .js file (CommonJS), so it cannot use require() on .ts files.

Before:

if base_config_path:
    require_path = f"./{base_config_path.name}"  # Generates require('./jest.config.ts')

After:

if base_config_path and base_config_path.suffix != ".ts":
    require_path = f"./{base_config_path.name}"  # Only require non-TypeScript configs

Solution

Check if base_config_path is a TypeScript file (.ts). If so, create a standalone runtime config without trying to extend it via require(). Jest will still discover and use the original TypeScript config naturally through its own resolution mechanism.

Testing

✅ Added comprehensive test in test_jest_typescript_config_bug.py

  • Creates realistic TypeScript Jest config with type annotations
  • Verifies generated runtime config loads without syntax errors
  • Confirms Node.js can successfully require the generated config

✅ All existing tests pass:

  • 34 JavaScript test runner tests
  • 2 new TypeScript config tests
  • No linting/type errors from uv run prek

Verification

To reproduce the original issue and verify the fix:

cd /workspace/target
codeflash --file packages/backend-core/src/users/utils.ts \
  --function isCreatorSync \
  --rerun 0fd176bf-5c7f-4f41-8396-77c46be86412

Before fix: Tests fail with "SyntaxError: Missing initializer in const declaration"
After fix: Tests run successfully


🤖 Generated with autonomous debug loop (iteration 1/20)

**Problem**: When a project uses `jest.config.ts` (TypeScript config), the
generated runtime config tries to `require('./jest.config.ts')`, which fails
because Node.js CommonJS cannot parse TypeScript syntax without compilation.

**Error**: `SyntaxError: Missing initializer in const declaration` at the
TypeScript type annotation (e.g., `const config: Config = ...`).

**Impact**: Affected 18 out of 38 optimization runs (~47%) in initial testing.
All TypeScript projects using `jest.config.ts` were unable to run tests.

**Root Cause**: Line 386 in test_runner.py used `base_config_path.name`
directly without checking the extension. The generated runtime config is
always a `.js` file, so it cannot use `require()` on `.ts` files.

**Solution**: Check if `base_config_path` is a TypeScript file (.ts). If so,
create a standalone runtime config without trying to extend it via require().
Jest will still discover and use the original TypeScript config naturally.

**Testing**:
- Added comprehensive test in test_jest_typescript_config_bug.py
- Test creates a realistic TypeScript Jest config and verifies the generated
  runtime config loads without syntax errors
- Existing 34 JavaScript test runner tests still pass
- No linting/type errors from `uv run prek`

**Trace IDs affected**: 0fd176bf-5c7f-4f41-8396-77c46be86412 and 17 others

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 4, 2026

Claude finished @mohammedahmed18's task in 2m 29s —— View job


PR Review Summary

PR size: SMALL — 5 lines of production code changed (test_runner.py +5/-1), plus a new test file (155 lines).


Prek Checks

uv run prek run --from-ref origin/mainpassed with no issues.


Code Review

The core fix is correct. Adding base_config_path.suffix != ".ts" prevents the require('./jest.config.ts') crash. The fallback to a standalone config is the right behavior given the constraints.

Issues found:

1. Testing convention violation — tempfile.TemporaryDirectory() must be replaced with tmp_path

Per .claude/rules/testing.md: "Use pytest's tmp_path fixture for temp directories — do not use tempfile.mkdtemp(), tempfile.TemporaryDirectory(), or NamedTemporaryFile."

Both test methods in test_jest_typescript_config_bug.py use tempfile.TemporaryDirectory(). Fix this →

2. Docstrings in test methods violate project conventions

Per CLAUDE.md: "Do not add docstrings to new or changed code unless the user explicitly asks for them." The test methods have multi-line docstrings. The class and module-level docstring are borderline; the per-method docstrings should be removed. Fix this →

3. Fix misses .mts extension

TypeScript ESM module config files use .mts. The check suffix != ".ts" would still fail on jest.config.mts. Low risk since .mts Jest configs are uncommon, but worth a follow-up. Suggest changing to:

if base_config_path and base_config_path.suffix not in {".ts", ".mts", ".cts"}:

4. Related bug in _create_codeflash_jest_config (out of scope, but worth noting)

test_runner.py:300-304 has the same pattern — require('{config_require_path}') where config_require_path can be a .ts file. This function is used for ESM compatibility wrappers. It isn't fixed here and will still fail for TypeScript config projects that also need ESM wrapping.


Duplicate Detection

No duplicates detected.


Last updated: 2026-04-04T05:14:00Z

@Saga4 Saga4 merged commit c0942b1 into main Apr 4, 2026
26 of 28 checks passed
@Saga4 Saga4 deleted the fix/typescript-jest-config-require branch April 4, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants