Repository navigation
fix: import() fallback prevention - #5647
Merged
Merged
Conversation
mark-wiemer
self-requested a review
January 13, 2026 01:48
JoshuaKGoldberg
approved these changes
Jan 21, 2026
JoshuaKGoldberg
left a comment
Member
There was a problem hiding this comment.
Makes sense to me, thanks! Since @mark-wiemer self-requested a review I'll defer to Mark.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
lennonnikolas
pushed a commit
to lennonnikolas/mocha
that referenced
this pull request
Jan 24, 2026
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
3 tasks done
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.
PR Checklist
status: accepting prsOverview
Problem
When using Mocha in parallel mode with TypeScript via ts-node (CJS mode), if a test file contains
.onlyand triggers aFORBIDDEN_EXCLUSIVITYerror, the actual error message was buried. Instead of showing:Users would see a confusing module loading error:
Root Cause
In
lib/nodejs/esm-utils.js, therequireModulefunction had a fallback mechanism: whenrequire()failed, it would attempt to use dynamicimport(). This was intended to handle ESM/CJS interop issues.However, when a Mocha-specific error (like
FORBIDDEN_EXCLUSIVITY) was thrown duringrequire(), it wasn't recognized as a "real" error that should be surfaced immediately. Instead, the code fell through to theimport()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 duringrequire(), it's now thrown immediately instead of falling back toimport().This follows the same pattern established in PR #5572, which added similar handling for ts-node's
TSError.Changes Made
Files Modified:
Code Changes:
isMochaErrorfrom../errorsif (isMochaError(requireErr)) { throw requireErr; }TSErrorcheck for consistencyTesting
FORBIDDEN_EXCLUSIVITYerror