fix: JS/TS module system and test framework detection#1289
Merged
Conversation
TypeScript files always use ESM syntax (import/export), so they should be detected as ESM even when package.json doesn't have "type": "module". Changes: - Check file extension first (.ts, .tsx, .mts = ESM) - Only use package.json "type" if explicitly set (no default) - Fall through to file content analysis when type not specified This fixes Jest test timeouts caused by module system mismatch.
When determining the module system for generated tests, pass the source file path so TypeScript files are correctly detected as ESM rather than falling through to the "Defaulting to CommonJS" default.
… jest The TestConfig.test_framework property was hardcoded to return "jest" for JavaScript projects. This fix uses the get_js_test_framework_or_default() function from the test_framework singleton, which properly returns the detected test framework from package.json (jest, vitest, or mocha).
misrasaurabh1
previously approved these changes
Feb 3, 2026
This optimization achieves a **10% runtime improvement** by eliminating redundant string scanning operations.
## Key Optimization
**Combined duplicate passes over test_globals**: The original code performed two separate passes checking if test globals appear in the code:
1. First pass: `needs_import = any(...)` to determine if *any* global is present
2. Second pass: `used_globals = [g for g in test_globals if ...]` to collect *which* globals are present
The optimized version merges these into a single pass that directly builds the `used_globals` list, eliminating the `needs_import` variable entirely. This cuts the number of substring searches (e.g., `f"{g}(" in code`) roughly in half.
## Why This Speeds Up Execution
In Python, string containment checks using the `in` operator are O(n×m) operations where n is the length of the haystack and m is the length of the needle. With 9 test globals and potentially large code strings, scanning twice through the code for each global was a significant bottleneck. The line profiler confirms this:
- Original: Lines checking `needs_import` and `used_globals` consumed **19.8%** of runtime (8.6% + 11.2%)
- Optimized: Single `used_globals` check consumes **15.9%** of runtime
## Test Case Performance
The optimization shows particularly strong gains on test cases that:
- Use multiple test globals (16-43% faster on individual global tests)
- Have larger code strings with many test cases (up to 37% faster on `test_vitest_adds_expect_import`)
- Process files with complex nesting and mixed content (15-27% improvements)
Early-exit paths (non-vitest frameworks, existing imports, no globals needed) remain fast since they bypass the optimization entirely, showing minimal overhead changes (0-3%).
This is a clean, focused optimization that reduces algorithmic complexity without changing behavior or code structure, making it safe to merge.
Contributor
⚡️ Codeflash found optimizations for this PR📄 11% (0.11x) speedup for
|
…2026-02-03T11.36.53 ⚡️ Speed up function `ensure_vitest_imports` by 11% in PR #1289 (`fix/js-module-detection`)
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
Test plan