fix: strip file extensions from JS/TS import paths in generated tests#1215
Merged
misrasaurabh1 merged 2 commits intomainfrom Jan 31, 2026
Merged
fix: strip file extensions from JS/TS import paths in generated tests#1215misrasaurabh1 merged 2 commits intomainfrom
misrasaurabh1 merged 2 commits intomainfrom
Conversation
LLMs often add .js extensions to TypeScript import paths (e.g.,
`import { func } from '../module.js'`), but TypeScript's module resolution
doesn't require explicit extensions. This causes "Cannot find module" errors
when Jest tries to resolve these imports.
This change adds `strip_js_extensions()` to remove .js/.ts/.tsx/.jsx/.mjs/.mts
extensions from relative import paths in generated tests. The function handles:
- ES module imports: import { x } from '../path/file.js'
- CommonJS requires: require('../path/file.js')
- Jest mocks: jest.mock('../path/file.js'), jest.doMock(), etc.
External package imports (lodash, react, etc.) and alias imports (@/components)
are preserved.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
e684ce7 to
92a18ed
Compare
The `add_js_extension()` function in `module_system.py` was adding .js extensions when converting CommonJS requires to ESM imports. This caused "Cannot find module" errors because TypeScript/Jest module resolution doesn't require explicit extensions. This change modifies `add_js_extension()` to NOT add extensions, as TypeScript and modern bundlers handle extension resolution automatically. Note: The aiservice now handles stripping any extensions that LLMs might add (see codeflash-internal PR #2341), so CLI-side stripping is removed. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
c53bd2c to
a3d6f47
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the root cause of "Cannot find module" errors when running JS/TS tests.
The
add_js_extension()function inmodule_system.pywas adding.jsextensions when converting CommonJSrequire()to ESMimportstatements. TypeScript and Jest don't require explicit extensions and adding them causes module resolution failures.Problem
When the CLI converted CommonJS to ESM:
This caused:
Solution
Modified
add_js_extension()to NOT add extensions. TypeScript and modern bundlers handle extension resolution automatically.Note: Extension stripping for LLM-generated tests is now handled by the aiservice (see codeflash-internal PR #2341).
Test plan
🤖 Generated with Claude Code