1515import pytest
1616
1717from codeflash .code_utils .code_replacer import replace_function_definitions_for_language
18+ from codeflash .languages .base import Language
19+ from codeflash .languages .current import set_current_language
1820from codeflash .languages .javascript .module_system import (
1921 ModuleSystem ,
2022 convert_commonjs_to_esm ,
@@ -300,57 +302,176 @@ def test_preserves_function_code(self):
300302 assert "return add(x, y);" in result
301303
302304
303- class TestModuleSystemCompatibility :
304- """Tests for module system compatibility."""
305+ class TestTsJestSkipsConversion :
306+ """Tests verifying that module system conversion is skipped when ts-jest is installed.
305307
306- def test_convert_mixed_code_to_esm (self ):
307- """Test converting mixed CJS/ESM code to pure ESM - exact output."""
308- code = """\
309- import { existing } from './module.js';
308+ When ts-jest is installed, it handles module interoperability internally,
309+ so we skip conversion to avoid breaking valid imports.
310+ """
311+ def __init__ (self ):
312+ set_current_language (Language .TYPESCRIPT )
313+
314+ def test_commonjs_not_converted_when_ts_jest_installed (self , tmp_path ):
315+ """Test that CommonJS is NOT converted to ESM when ts-jest is installed."""
316+ # Create a project with ts-jest
317+ package_json = tmp_path / "package.json"
318+ package_json .write_text ('{"devDependencies": {"ts-jest": "^29.0.0"}}' )
319+
320+ commonjs_test = """\
321+ const Logger = require('../utils/logger');
322+ const { helper } = require('../utils/helpers');
323+
324+ describe('Logger', () => {
325+ test('should work', () => {
326+ const logger = new Logger();
327+ expect(logger).toBeDefined();
328+ });
329+ });
330+ """
331+ # With ts-jest, no conversion should happen
332+ result = ensure_module_system_compatibility (commonjs_test , ModuleSystem .ES_MODULE , tmp_path )
333+
334+ assert result == commonjs_test , (
335+ f"CommonJS should NOT be converted when ts-jest is installed.\n "
336+ f"Expected (unchanged):\n { commonjs_test } \n \n Got:\n { result } "
337+ )
338+
339+ def test_esm_not_converted_when_ts_jest_installed (self , tmp_path ):
340+ """Test that ESM is NOT converted to CommonJS when ts-jest is installed."""
341+ # Create a project with ts-jest
342+ package_json = tmp_path / "package.json"
343+ package_json .write_text ('{"devDependencies": {"ts-jest": "^29.0.0"}}' )
344+
345+ esm_test = """\
346+ import Logger from '../utils/logger';
347+ import { helper } from '../utils/helpers';
348+
349+ describe('Logger', () => {
350+ test('should work', () => {
351+ const logger = new Logger();
352+ expect(logger).toBeDefined();
353+ });
354+ });
355+ """
356+ # With ts-jest, no conversion should happen
357+ result = ensure_module_system_compatibility (esm_test , ModuleSystem .COMMONJS , tmp_path )
358+
359+ assert result == esm_test , (
360+ f"ESM should NOT be converted when ts-jest is installed.\n "
361+ f"Expected (unchanged):\n { esm_test } \n \n Got:\n { result } "
362+ )
363+
364+ def test_ts_jest_detected_in_jest_config (self , tmp_path ):
365+ """Test that ts-jest is detected from jest.config.js content."""
366+ # Create a project with ts-jest in jest.config.js (not package.json)
367+ package_json = tmp_path / "package.json"
368+ package_json .write_text ('{"devDependencies": {}}' )
369+ jest_config = tmp_path / "jest.config.js"
370+ jest_config .write_text ("module.exports = { preset: 'ts-jest' };" )
371+
372+ commonjs_test = "const x = require('./module');"
373+
374+ result = ensure_module_system_compatibility (commonjs_test , ModuleSystem .ES_MODULE , tmp_path )
375+
376+ assert result == commonjs_test , "Should skip conversion when ts-jest is in jest.config.js"
377+
378+
379+ class TestModuleSystemConversion :
380+ """Tests for module system conversion when ts-jest is NOT installed.
381+
382+ Without ts-jest, we convert between CommonJS and ESM as needed.
383+ """
384+
385+ def test_commonjs_converted_to_esm_without_ts_jest (self , tmp_path ):
386+ """Test that CommonJS is converted to ESM when ts-jest is NOT installed."""
387+ # Create a project WITHOUT ts-jest
388+ package_json = tmp_path / "package.json"
389+ package_json .write_text ('{"devDependencies": {"jest": "^29.0.0"}}' )
390+
391+ commonjs_code = """\
310392 const { helper } = require('./helpers');
393+ const logger = require('./logger');
311394
312395function process() {
313- return existing() + helper();
396+ return helper();
314397}
315398"""
316- result = ensure_module_system_compatibility (code , ModuleSystem .ES_MODULE )
399+ result = ensure_module_system_compatibility (commonjs_code , ModuleSystem .ES_MODULE , tmp_path )
317400
318- # Should convert require to import
401+ # Should be converted to ESM
319402 assert "import { helper } from './helpers';" in result
320- assert "require" not in result , f"require should be converted to import. Got:\n { result } "
403+ assert "import logger from './logger';" in result
404+ assert "require(" not in result
321405
322- def test_convert_mixed_code_to_commonjs (self ):
323- """Test converting mixed ESM/CJS code to pure CommonJS - exact output."""
324- code = """\
325- const { existing } = require('./module');
326- import { helper } from './helpers.js';
406+ def test_esm_converted_to_commonjs_without_ts_jest (self , tmp_path ):
407+ """Test that ESM is converted to CommonJS when ts-jest is NOT installed."""
408+ # Create a project WITHOUT ts-jest
409+ package_json = tmp_path / "package.json"
410+ package_json .write_text ('{"devDependencies": {"jest": "^29.0.0"}}' )
411+
412+ esm_code = """\
413+ import { helper } from './helpers';
414+ import logger from './logger';
327415
328416function process() {
329- return existing() + helper();
417+ return helper();
330418}
331419"""
332- result = ensure_module_system_compatibility (code , ModuleSystem .COMMONJS )
420+ result = ensure_module_system_compatibility (esm_code , ModuleSystem .COMMONJS , tmp_path )
333421
334- # Should convert import to require
422+ # Should be converted to CommonJS
335423 assert "const { helper } = require('./helpers');" in result
336- assert "import " not in result .split ("\n " )[0 ] or "import " not in result , (
337- f"import should be converted to require. Got:\n { result } "
338- )
424+ assert "const logger = require('./logger');" in result
425+ assert "import " not in result
426+
427+ def test_no_conversion_when_project_root_is_none (self ):
428+ """Test that conversion happens when project_root is None (can't detect ts-jest)."""
429+ commonjs_code = "const x = require('./module');"
430+
431+ # Without project_root, we can't detect ts-jest, so conversion should happen
432+ result = ensure_module_system_compatibility (commonjs_code , ModuleSystem .ES_MODULE , None )
433+
434+ # Should be converted to ESM
435+ assert "import x from './module';" in result
436+
437+ def test_mixed_code_not_converted (self , tmp_path ):
438+ """Test that mixed CJS/ESM code is NOT converted (already has both)."""
439+ package_json = tmp_path / "package.json"
440+ package_json .write_text ('{"devDependencies": {"jest": "^29.0.0"}}' )
339441
340- def test_pure_esm_unchanged (self ):
442+ mixed_code = """\
443+ import { existing } from './module.js';
444+ const { helper } = require('./helpers');
445+
446+ function process() {
447+ return existing() + helper();
448+ }
449+ """
450+ # Mixed code has both import and require, so no conversion
451+ result = ensure_module_system_compatibility (mixed_code , ModuleSystem .ES_MODULE , tmp_path )
452+
453+ assert result == mixed_code , "Mixed code should not be converted"
454+
455+ def test_pure_esm_unchanged_for_esm_target (self , tmp_path ):
341456 """Test that pure ESM code is unchanged when targeting ESM."""
457+ package_json = tmp_path / "package.json"
458+ package_json .write_text ('{"devDependencies": {"jest": "^29.0.0"}}' )
459+
342460 code = """\
343461 import { add } from './math.js';
344462
345463export function sum(a, b) {
346464 return add(a, b);
347465}
348466"""
349- result = ensure_module_system_compatibility (code , ModuleSystem .ES_MODULE )
350- assert result == code , f "Pure ESM code should be unchanged. \n Expected: \n { code } \n \n Got: \n { result } "
467+ result = ensure_module_system_compatibility (code , ModuleSystem .ES_MODULE , tmp_path )
468+ assert result == code , "Pure ESM code should be unchanged for ESM target "
351469
352- def test_pure_commonjs_unchanged (self ):
470+ def test_pure_commonjs_unchanged_for_commonjs_target (self , tmp_path ):
353471 """Test that pure CommonJS code is unchanged when targeting CommonJS."""
472+ package_json = tmp_path / "package.json"
473+ package_json .write_text ('{"devDependencies": {"jest": "^29.0.0"}}' )
474+
354475 code = """\
355476 const { add } = require('./math');
356477
@@ -360,8 +481,8 @@ def test_pure_commonjs_unchanged(self):
360481
361482module.exports = { sum };
362483"""
363- result = ensure_module_system_compatibility (code , ModuleSystem .COMMONJS )
364- assert result == code , f "Pure CommonJS code should be unchanged. \n Expected: \n { code } \n \n Got: \n { result } "
484+ result = ensure_module_system_compatibility (code , ModuleSystem .COMMONJS , tmp_path )
485+ assert result == code , "Pure CommonJS code should be unchanged for CommonJS target "
365486
366487
367488class TestImportStatementGeneration :
0 commit comments