Skip to content

fix/js vitest benchmarking and mocha cjs#1737

Merged
Saga4 merged 3 commits intomainfrom
fix/js-vitest-benchmarking-and-mocha-cjs
Mar 3, 2026
Merged

fix/js vitest benchmarking and mocha cjs#1737
Saga4 merged 3 commits intomainfrom
fix/js-vitest-benchmarking-and-mocha-cjs

Conversation

@Saga4
Copy link
Contributor

@Saga4 Saga4 commented Mar 3, 2026

  • fix: use pattern matching for collocated tests in monorepos
  • fix: support Mocha CJS projects and sanitize incorrect framework imports
  • fix: resolve Vitest benchmarking showing wall-clock time instead of per-function timing

Saga4 and others added 3 commits March 3, 2026 14:29
When testsRoot overlaps moduleRoot (common in JS/TS monorepos like Ghost
where both point to "src"), the directory-based filter incorrectly
excluded ALL source files. Switch to filename/directory pattern matching
(*.test.*, *.spec.*, __tests__/) when roots overlap, preserving the
existing directory-based filter for standard layouts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Three related fixes for Mocha test generation in CommonJS projects:

1. inject_test_globals() now accepts module_system param — emits
   `require('node:assert/strict')` for CJS instead of ESM import syntax
2. ensure_module_system_compatibility() now converts ESM→CJS even when
   the source has mixed imports (was skipping when both ESM and CJS were
   detected, leaving the ESM import from inject_test_globals unconverted)
3. New sanitize_mocha_imports() strips vitest/jest/@jest/globals imports
   that the AI sometimes generates for Mocha projects — Mocha provides
   describe/it/before*/after* as globals

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…er-function timing

Root cause: Vitest performance tests reported "20.0 seconds over 1 loop"
(JUnit XML wall-clock fallback) instead of actual per-function nanosecond
timing. This was a chain of two issues:

1. **stdout interception**: Vitest's default `threads` pool intercepts
   process.stdout.write() and console.log(), preventing timing markers
   from flowing to the parent process. Fixed by adding `--pool=forks`
   to all Vitest commands and config files. The `forks` pool uses child
   processes where stdout flows directly to the parent.

2. **test name detection**: Even after markers flowed through (43,000+
   found in stdout), the parser couldn't match them to JUnit XML
   testcases because all markers had "unknown" as the test name. This
   happened because Vitest doesn't inject `beforeEach` as a global
   (unlike Jest), so capture.js's Jest-style hook to set
   `currentTestName` never fired.

   Fixed by adding Vitest-specific test name detection in capture.js:
   - Primary: `expect.getState().currentTestName` (full describe path)
   - Fallback: `__vitest_worker__.current.fullTestName`
   - Defense-in-depth: parser fallback matches "unknown" markers to
     the first testcase when no name match is found

Result: cheerio's `isHtml` went from "20.0s / 1 loop" to
"902μs / 20,853 loops" with proper speedup analysis.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Saga4 Saga4 requested a review from mohammedahmed18 March 3, 2026 10:01
@claude
Copy link
Contributor

claude bot commented Mar 3, 2026

Claude encountered an error —— View job


PR Review Summary

Tasks

  • Triage PR scope
  • Lint and typecheck
  • Resolve stale threads
  • Code review
  • Duplicate detection
  • Test coverage
  • Simplify
  • Merge optimization PRs

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Mar 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 98% (0.98x) speedup for _is_test_file_by_pattern in codeflash/discovery/functions_to_optimize.py

⏱️ Runtime : 8.97 milliseconds 4.52 milliseconds (best of 114 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-vitest-benchmarking-and-mocha-cjs).

Static Badge

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Mar 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 63% (0.63x) speedup for _is_test_file_by_pattern in codeflash/tracing/tracing_utils.py

⏱️ Runtime : 1.96 milliseconds 1.20 milliseconds (best of 134 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-vitest-benchmarking-and-mocha-cjs).

Static Badge

@Saga4 Saga4 merged commit 52210f6 into main Mar 3, 2026
24 of 28 checks passed
@Saga4 Saga4 deleted the fix/js-vitest-benchmarking-and-mocha-cjs branch March 3, 2026 18:48
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.

1 participant