Skip to content

fix: import() fallback prevention - #5647

Merged
mark-wiemer merged 3 commits into
mochajs:mainfrom
darthvader58:pr
Jan 23, 2026
Merged

mark-wiemer merged 3 commits into
mochajs:mainfrom
darthvader58:pr

Conversation

@darthvader58

Copy link
Copy Markdown
Contributor

PR Checklist

Overview

Problem

When using Mocha in parallel mode with TypeScript via ts-node (CJS mode), if a test file contains .only and triggers a FORBIDDEN_EXCLUSIVITY error, the actual error message was buried. Instead of showing:

`.only` is not supported in parallel mode

Users would see a confusing module loading error:

Cannot use import statement outside a module

Root Cause

In lib/nodejs/esm-utils.js, the requireModule function had a fallback mechanism: when require() failed, it would attempt to use dynamic import(). This was intended to handle ESM/CJS interop issues.

However, when a Mocha-specific error (like FORBIDDEN_EXCLUSIVITY) was thrown during require(), it wasn't recognized as a "real" error that should be surfaced immediately. Instead, the code fell through to the import() fallback, which bypassed ts-node's CJS loader and produced a misleading syntax error.

Solution

Added a check to detect Mocha errors using the existing isMochaError() utility function. When a Mocha error is caught during require(), it's now thrown immediately instead of falling back to import().

This follows the same pattern established in PR #5572, which added similar handling for ts-node's TSError.

Changes Made

Files Modified:

  1. lib/nodejs/esm-utils.js - Added Mocha error detection
  2. test/node-unit/esm-utils.spec.js - Added test case
  3. test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts - Created test fixture

Code Changes:

  • Imported isMochaError from ../errors
  • Added check: if (isMochaError(requireErr)) { throw requireErr; }
  • Positioned after the existing TSError check for consistency

Testing

  • Created a test fixture that simulates the FORBIDDEN_EXCLUSIVITY error
  • Added test case verifying Mocha errors are surfaced correctly
  • All 580 existing tests continue to pass

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jan 11, 2026 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@mark-wiemer
mark-wiemer self-requested a review January 13, 2026 01:48
@mark-wiemer mark-wiemer self-assigned this Jan 13, 2026
@mark-wiemer mark-wiemer added the status: needs review a maintainer should (re-)review this pull request label Jan 13, 2026

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, thanks! Since @mark-wiemer self-requested a review I'll defer to Mark.

Comment thread lib/nodejs/esm-utils.js Resolved
@JoshuaKGoldberg JoshuaKGoldberg changed the title Fix: import() fallback prevention fix: import() fallback prevention Jan 21, 2026
TSError handler

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
@codecov

codecov Bot commented Jan 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.40%. Comparing base (e06cce7) to head (556f25f).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5647   +/-   ##
=======================================
  Coverage   88.40%   88.40%           
=======================================
  Files          66       66           
  Lines        4787     4788    +1     
  Branches      977      977           
=======================================
+ Hits         4232     4233    +1     
  Misses        555      555           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mark-wiemer mark-wiemer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mark-wiemer
mark-wiemer merged commit 6a78fa3 into mochajs:main Jan 23, 2026
79 checks passed
@mark-wiemer mark-wiemer removed the status: needs review a maintainer should (re-)review this pull request label Jan 23, 2026
@darthvader58
darthvader58 deleted the pr branch January 23, 2026 06:39
lennonnikolas pushed a commit to lennonnikolas/mocha that referenced this pull request Jan 24, 2026
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
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.

🐛 Bug: '.only' in parallel, typscript test gives confusing error

3 participants