You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that when using import-in-the-middle with mocha, mocha exits without running tests as require.main === module evaluates to false when it's trying to determine if it is run directly from Node.js.
We wrap all modules in the loader with a wrapper module that allows for modification of exports. We could exclude CJS entrypoints, but that would create an edge case in which they would not be hook-able. In practice, that shouldn't break much, since entrypoints are rarely intentionally hooked, but that would constitute a breaking change if not behind a flag.
For now, you can work around this by wrapping the IITM hook in your own that would look something like this:
// iitm hook that preserves require.main === moduleimport{load,resolveasiitmResolve}from'import-in-the-middle/hook.mjs'asyncfunctionresolve(specifier,context,parentResolve){if(!context.parentURL){constresolved=awaitparentResolve(specifier,context)if(resolved.format==='commonjs'){returnresolved}}returniitmResolve(specifier,context,parentResolve)}export{load,resolve}
In a future release, we may exclude CJS entrypoints from wrapping. It might be behind a flag, in order to not break the aforementioned edge case.
It seems that when using
import-in-the-middle
withmocha
,mocha
exits without running tests asrequire.main === module
evaluates tofalse
when it's trying to determine if it is run directly from Node.js.Expected Behavior
mocha
runs tests even if IITM is usedActual Behavior
mocha
exists with0
, not running any testsSteps to Reproduce the Problem
I've created a reproducer to illustrate that contains a more detailed description:
https://github.com/pichlermarc/esm-test
It contains two scripts, both attempt to run a single test that always fails, one with the IITM loader, and one without.
git clone https://github.com/pichlermarc/esm-test
npm install
npm run test
- this does not use the IITM hook, tests do run, and mocha exits with
1
(as expected)npm run test:iitm
- this uses the IITM hook, tests do not run, and
mocha
exits with0
(1
is expected)Specifications
[email protected]
[email protected]
The text was updated successfully, but these errors were encountered: