@@ -2059,9 +2059,9 @@ def process_generated_test_strings(
20592059 generated_test_source , project_module_system , test_cfg .tests_project_rootdir
20602060 )
20612061
2062- # Add .js extensions to relative imports for ESM projects
2063- # TypeScript + ESM requires explicit .js extensions even for .ts source files
2064- if project_module_system == ModuleSystem .ES_MODULE :
2062+ # Add .js extensions to relative imports for ESM projects — but NOT for Jest,
2063+ # which resolves .ts imports without .js extensions via its transform/resolver.
2064+ if project_module_system == ModuleSystem .ES_MODULE and test_cfg . test_framework != "jest" :
20652065 from codeflash .languages .javascript .module_system import add_js_extensions_to_relative_imports
20662066
20672067 generated_test_source = add_js_extensions_to_relative_imports (generated_test_source )
@@ -2264,10 +2264,13 @@ def get_module_path(self, source_file: Path, project_root: Path, tests_root: Pat
22642264 # (e.g. \t → tab, \n → newline) and would break imports on Windows.
22652265 rel_path = os .path .relpath (str (source_without_ext ), str (tests_root_abs )).replace ("\\ " , "/" )
22662266
2267- # For ESM, add .js extension (TypeScript convention)
2268- # TypeScript requires imports to reference the OUTPUT file extension (.js),
2269- # even when the source file is .ts. This is required for Node.js ESM resolution.
2270- if module_system == ModuleSystem .ES_MODULE :
2267+ # For ESM, add .js extension (TypeScript convention) — but only for Vitest/native ESM.
2268+ # Jest resolves .ts imports without .js extensions via its transform/resolver config,
2269+ # so adding .js breaks Jest module resolution (Cannot find module '../foo.js').
2270+ from codeflash .languages .test_framework import get_js_test_framework_or_default
2271+
2272+ test_framework = get_js_test_framework_or_default ()
2273+ if module_system == ModuleSystem .ES_MODULE and test_framework != "jest" :
22712274 rel_path = rel_path + ".js"
22722275 logger .debug (
22732276 f"!lsp|Module path (ESM): source={ source_file_abs } , tests_root={ tests_root_abs } , "
@@ -2286,7 +2289,7 @@ def get_module_path(self, source_file: Path, project_root: Path, tests_root: Pat
22862289 # For fallback, also check module system
22872290 module_system = detect_module_system (project_root , source_file )
22882291 path_without_ext = "../" + rel_path .with_suffix ("" ).as_posix ()
2289- if module_system == ModuleSystem .ES_MODULE :
2292+ if module_system == ModuleSystem .ES_MODULE and test_framework != "jest" :
22902293 return path_without_ext + ".js"
22912294 return path_without_ext
22922295
0 commit comments