Skip to content

Fix: Add TypeScript transformer to runtime Jest config#2011

Open
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/typescript-runtime-transform
Open

Fix: Add TypeScript transformer to runtime Jest config#2011
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/typescript-runtime-transform

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

When Jest base config is TypeScript (.ts file), all TypeScript tests fail with syntax errors. This affects 23 out of 45 logs (~51%) - a systematic high-severity bug blocking all TypeScript optimizations.

Error:

SyntaxError: Unexpected token, expected "," (37:45)
> 37 | export function addDatasourceFlags(datasource: Datasource) {
     |                                              ^

Root Cause

The _create_runtime_jest_config() function creates a standalone config when the base config is .ts (cannot be directly required by Node.js). This standalone config was missing the TypeScript transformer configuration entirely.

Bug location: codeflash/languages/javascript/test_runner.py:668-696

Flow:

  1. Jest base config is jest.config.ts (TypeScript)
  2. _create_runtime_jest_config() detects .ts extension (line 642)
  3. Creates standalone config (lines 688-696) to avoid requiring .ts file
  4. Standalone config only includes: roots, testMatch, moduleNameMapper
  5. NO transform configuration → Jest cannot parse TypeScript → Syntax errors

Fix

Added TypeScript transformer detection to standalone config creation path:

  • Call _detect_typescript_transformer() to find ts-jest/@swc/jest/babel
  • Inject transform config into the standalone runtime Jest config
  • Preserves existing behavior when transformer is not available

Files Changed

  • codeflash/languages/javascript/test_runner.py (12 lines added at lines 688-699)
  • tests/test_languages/test_typescript_runtime_config_transform.py (new file, 137 lines, 4 tests)

Testing

✅ 4 new regression tests:

  • test_runtime_config_includes_typescript_transform_with_ts_jest
  • test_runtime_config_includes_typescript_transform_with_swc
  • test_runtime_config_includes_typescript_transform_with_babel_fallback
  • test_runtime_config_no_transform_when_no_typescript_transformer

✅ All 34 existing test_javascript_test_runner.py tests pass
✅ No linting/type errors (uv run prek passes)

Impact

Severity: HIGH
Fixes: 23 out of 45 failing optimization runs (~51%)
Trace IDs: 1f944efe-bdab-4f1e-81cc-19189023c81a (and 22 others)

Unblocks all TypeScript optimizations for projects using .ts Jest configs.

Verification

Before fix (runtime config):

module.exports = {
  roots: ["/path/to/project", "/path/to/tests"],
  testMatch: ['**/*.test.ts', ...],
  // NO transform - TypeScript fails!
};

After fix (runtime config):

module.exports = {
  roots: ["/path/to/project", "/path/to/tests"],
  testMatch: ['**/*.test.ts', ...],
  transform: {
    '^.+\\.(ts|tsx)$': ['ts-jest', { isolatedModules: true }],
  }, // ← Now includes TypeScript transformer!
};

**Problem:**
When Jest base config is TypeScript (.ts file), all TypeScript tests fail
with syntax errors like "Unexpected token, expected ','". This affects 23
out of 45 optimization runs (~51%).

**Root Cause:**
The _create_runtime_jest_config() function creates a standalone config when
base config is .ts (cannot be directly required by Node.js). This standalone
config was missing the TypeScript transformer configuration entirely, so Jest
tried to execute TypeScript code as plain JavaScript.

**Error:**
```
SyntaxError: Unexpected token, expected "," (37:45)
> 37 | export function addDatasourceFlags(datasource: Datasource) {
     |                                              ^
```

**Fix:**
Added TypeScript transformer detection to standalone config creation path:
- Call _detect_typescript_transformer() to find ts-jest/@swc/jest/babel
- Inject transform config into the standalone runtime Jest config
- Preserves existing behavior when transformer is not available

**Files Changed:**
- codeflash/languages/javascript/test_runner.py (12 lines added)
- tests/test_languages/test_typescript_runtime_config_transform.py (new, 137 lines)

**Testing:**
- 4 new unit tests for transform injection with ts-jest, @swc/jest, babel
- All 34 existing test_runner tests pass
- No linting/type errors

**Impact:** Fixes TypeScript optimizations for all projects using .ts Jest configs

**Trace IDs:** 1f944efe-bdab-4f1e-81cc-19189023c81a (and 22 others)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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.

1 participant