Skip to content

Commit e8a4f96

Browse files
Saga4claude
andcommitted
fix: strip CJS require('vitest') and require('@jest/globals') in Mocha tests
The AI backend generates vitest/jest-style imports for Mocha projects. Our sanitize_mocha_imports() stripped ESM `import { ... } from 'vitest'`, but process_generated_test_strings() runs BEFORE postprocessing and calls ensure_module_system_compatibility() which converts these to CJS requires. Result: `const { ... } = require('vitest')` survived sanitization. Added regexes for the CJS variants of vitest and @jest/globals requires. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ed2594b commit e8a4f96

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

codeflash/languages/javascript/edit_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ def inject_test_globals(
233233

234234

235235
_VITEST_IMPORT_RE = re.compile(r"^.*import\s+\{[^}]*\}\s+from\s+['\"]vitest['\"].*\n?", re.MULTILINE)
236+
_VITEST_REQUIRE_RE = re.compile(
237+
r"^.*(?:const|let|var)\s+\{[^}]*\}\s*=\s*require\s*\(\s*['\"]vitest['\"]\s*\).*\n?", re.MULTILINE
238+
)
236239
_JEST_GLOBALS_IMPORT_RE = re.compile(r"^.*import\s+\{[^}]*\}\s+from\s+['\"]@jest/globals['\"].*\n?", re.MULTILINE)
240+
_JEST_GLOBALS_REQUIRE_RE = re.compile(
241+
r"^.*(?:const|let|var)\s+\{[^}]*\}\s*=\s*require\s*\(\s*['\"]@jest/globals['\"]\s*\).*\n?", re.MULTILINE
242+
)
237243
_MOCHA_REQUIRE_RE = re.compile(
238244
r"^.*(?:const|let|var)\s+\{[^}]*\}\s*=\s*require\s*\(\s*['\"]mocha['\"]\s*\).*\n?", re.MULTILINE
239245
)
@@ -256,7 +262,9 @@ def sanitize_mocha_imports(source: str) -> str:
256262
257263
"""
258264
source = _VITEST_IMPORT_RE.sub("", source)
265+
source = _VITEST_REQUIRE_RE.sub("", source)
259266
source = _JEST_GLOBALS_IMPORT_RE.sub("", source)
267+
source = _JEST_GLOBALS_REQUIRE_RE.sub("", source)
260268
source = _MOCHA_REQUIRE_RE.sub("", source)
261269
return _VITEST_COMMENT_RE.sub("", source)
262270

tests/test_languages/test_mocha_runner.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,30 @@ def test_strips_vitest_comment(self):
491491
assert "vitest" not in result
492492
assert "const x = 1;" in result
493493

494+
def test_strips_vitest_require_cjs(self):
495+
from codeflash.languages.javascript.edit_tests import sanitize_mocha_imports
496+
497+
source = "const { describe, test, expect, vi, beforeEach, afterEach } = require('vitest');\nconst x = 1;\n"
498+
result = sanitize_mocha_imports(source)
499+
assert "vitest" not in result
500+
assert "const x = 1;" in result
501+
502+
def test_strips_jest_globals_require_cjs(self):
503+
from codeflash.languages.javascript.edit_tests import sanitize_mocha_imports
504+
505+
source = "const { jest, describe, it } = require('@jest/globals');\nconst x = 1;\n"
506+
result = sanitize_mocha_imports(source)
507+
assert "@jest/globals" not in result
508+
assert "const x = 1;" in result
509+
510+
def test_strips_vitest_comment_and_cjs_require(self):
511+
from codeflash.languages.javascript.edit_tests import sanitize_mocha_imports
512+
513+
source = "// vitest imports (REQUIRED for vitest - globals are NOT enabled by default)\nconst { describe, test, expect, vi, beforeEach, afterEach } = require('vitest');\nconst { setCharset } = require('../lib/utils');\n"
514+
result = sanitize_mocha_imports(source)
515+
assert "vitest" not in result
516+
assert "require('../lib/utils')" in result
517+
494518
def test_preserves_unrelated_imports(self):
495519
from codeflash.languages.javascript.edit_tests import sanitize_mocha_imports
496520

0 commit comments

Comments
 (0)