Skip to content

fix(js): detect TypeScript transformers beyond ts-jest#2003

Open
mohammedahmed18 wants to merge 2 commits intomainfrom
fix/typescript-transform-detection
Open

fix(js): detect TypeScript transformers beyond ts-jest#2003
mohammedahmed18 wants to merge 2 commits intomainfrom
fix/typescript-transform-detection

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

When a TypeScript project uses transformers other than ts-jest (e.g., @swc/jest, babel-jest, esbuild-jest), Codeflash generates a Jest config without a transform directive. This causes Jest to fail parsing TypeScript files with syntax errors like "Unexpected token" at type annotations.

Example error:

SyntaxError: /path/to/file.ts: Unexpected token (5:6)
  3 |
  4 | export default class API {
> 5 |   host: string
    |       ^

Root Cause

File: codeflash/languages/javascript/test_runner.py:282-295

The code only checked for ts-jest in package.json and skipped transform configuration when ts-jest was not found, even though other TypeScript transformers might be present:

has_ts_jest = _has_ts_jest_dependency(project_root)

if has_ts_jest:
    transform_config = """..."""
else:
    transform_config = ""  # ← No transform = Jest can't parse TypeScript

Solution

Added _detect_typescript_transformer() function that checks for common TypeScript transformers in order of preference:

  1. ts-jest
  2. @swc/jest
  3. babel-jest (with @babel/preset-typescript)
  4. esbuild-jest

When detected, the appropriate transform directive is included in the generated Jest config.

Impact

Fixes: TypeScript projects using @swc/jest (e.g., budibase)
Fixes: TypeScript projects using babel-jest
Fixes: TypeScript projects using esbuild-jest
Maintains: Backward compatibility with ts-jest projects

Testing

  • Added test_typescript_transform_extraction.py with two test cases
  • All existing test_javascript_test_runner.py tests pass (34 tests)
  • Verified fix against budibase trace ID 0acc4768-706a-4fe1-91e6-ec64983bd519

Trace IDs

  • 0acc4768-706a-4fe1-91e6-ec64983bd519 - budibase API.patch optimization failure

**Problem:**
When a TypeScript project uses transformers other than ts-jest (e.g.,
@swc/jest, babel-jest, esbuild-jest), Codeflash would generate a Jest
config without a transform directive. This caused Jest to fail parsing
TypeScript files with syntax errors like "Unexpected token" at type
annotations.

**Root Cause:**
File: codeflash/languages/javascript/test_runner.py:282-295
The code only checked for ts-jest in package.json and skipped transform
configuration when ts-jest was not found, even though other TypeScript
transformers might be present.

**Solution:**
Added _detect_typescript_transformer() function that checks for common
TypeScript transformers in order of preference:
1. ts-jest
2. @swc/jest
3. babel-jest (with @babel/preset-typescript)
4. esbuild-jest

When detected, the appropriate transform directive is included in the
generated Jest config, enabling Jest to parse TypeScript files.

**Impact:**
- Fixes optimization failures for TypeScript projects using @swc/jest
  (e.g., budibase)
- Fixes optimization failures for TypeScript projects using babel-jest
- Fixes optimization failures for TypeScript projects using esbuild-jest
- Maintains backward compatibility with ts-jest projects

**Testing:**
- Added test_typescript_transform_extraction.py with two test cases
- All existing test_javascript_test_runner.py tests pass (34 tests)

**Trace IDs:**
- 0acc4768-706a-4fe1-91e6-ec64983bd519 (budibase API.patch optimization)

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

claude bot commented Apr 6, 2026

Claude finished @mohammedahmed18's task in 4m 40s —— View job


PR Review Summary

  • Triage PR size (LARGE: ~60 lines production code)
  • Lint and typecheck — prek passed; fixed mypy issues in test file
  • Resolve stale threads (none)
  • Auto-fix and commit style issues
  • Review diff
  • Duplicate detection

Prek Checks

Passed. ✅

Auto-fixed and pushed (aa463709):

  • Added encoding="utf-8" to read_text() in new _detect_typescript_transformer — required by project convention for all new code
  • Removed docstring from _detect_typescript_transformer — project style disallows docstrings in new code unless explicitly requested
  • Test file: replaced tempfile.TemporaryDirectory() with tmp_path fixture — project testing convention
  • Test file: added -> None return type annotations (mypy errors)
  • Test file: removed unused import pytest and class/method docstrings

Code Review

Bug / Dead Code — must fix

_has_ts_jest_dependency is dead code (test_runner.py:200).

The PR replaces all call sites of _has_ts_jest_dependency with the new _detect_typescript_transformer, but leaves the old function in place. It is now unreachable and should be removed.

Fix this →

Design Issue — consider fixing

Triplicate package.json parsing for ts-jest (test_runner.py:200, 222, 678).

There are now three separate functions that each open package.json and check for ts-jest:

  • _has_ts_jest_dependency (dead, see above)
  • _detect_typescript_transformer (new — checks deps for 4 transformers)
  • _uses_ts_jest (line 678, still actively called at line 737 — also checks jest.config files)

_detect_typescript_transformer and _uses_ts_jest solve overlapping problems: both detect "is ts-jest present?". Consider having _detect_typescript_transformer reuse _uses_ts_jest's result, or consolidate the detection into one function.

Minor — no action needed

The babel-jest detection at line 265 requires both babel-jest and @babel/preset-typescript to be present. This is a reasonable safety guard (babel-jest alone won't handle TypeScript), but it's worth noting as a potential miss if a project uses @babel/preset-typescript via babel.config.js without listing it explicitly in package.json.


Duplicate Detection

  • HIGH confidence: _has_ts_jest_dependency (line 200) vs _detect_typescript_transformer (line 222) — identical package.json parsing pattern, ts-jest detection logic is a strict subset. The former is now dead code.
  • MEDIUM confidence: _detect_typescript_transformer (line 222) vs _uses_ts_jest (line 678) — both parse package.json for ts-jest; _uses_ts_jest additionally checks jest.config files.

Test Coverage

Two integration tests added covering @swc/jest and ts-jest paths. The babel-jest and esbuild-jest branches have no test coverage — acceptable for now but worth noting.


Last updated: 2026-04-06T11:49Z
· Branch

- Use tmp_path fixture instead of tempfile.TemporaryDirectory (project convention)
- Add -> None return type annotations to test methods
- Add encoding="utf-8" to read_text/write_text calls in new code
- Remove docstring from _detect_typescript_transformer (project style)
- Remove unused import pytest from test file

Co-authored-by: mohammed ahmed <undefined@users.noreply.github.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