Skip to content

fix: JS/TS module system and test framework detection#1289

Merged
KRRT7 merged 8 commits into
mainfrom
fix/js-module-detection
Feb 3, 2026
Merged

fix: JS/TS module system and test framework detection#1289
KRRT7 merged 8 commits into
mainfrom
fix/js-module-detection

Conversation

@KRRT7

@KRRT7 KRRT7 commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix TypeScript files incorrectly detected as CommonJS instead of ESM
  • Fix test framework detection ignoring package.json devDependencies (was hardcoded to Jest)

Test plan

  • Verified TypeScript files are detected as ESM regardless of package.json type field
  • Verified test framework detection correctly returns mocha/jest/vitest from devDependencies
  • All existing tests pass

KRRT7 added 3 commits February 3, 2026 00:59
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
misrasaurabh1 previously approved these changes Feb 3, 2026
KRRT7 and others added 2 commits February 3, 2026 06:29
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.
@codeflash-ai

codeflash-ai Bot commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

⚡️ Codeflash found optimizations for this PR

📄 11% (0.11x) speedup for ensure_vitest_imports in codeflash/languages/javascript/module_system.py

⏱️ Runtime : 722 microseconds 652 microseconds (best of 153 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch fix/js-module-detection).

Static Badge

KRRT7 and others added 2 commits February 3, 2026 06:37
…2026-02-03T11.36.53

⚡️ Speed up function `ensure_vitest_imports` by 11% in PR #1289 (`fix/js-module-detection`)
@KRRT7 KRRT7 merged commit acd4028 into main Feb 3, 2026
22 of 23 checks passed
@KRRT7 KRRT7 deleted the fix/js-module-detection branch February 3, 2026 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants