Open pichlermarc opened 10 months ago
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 === module
import { load, resolve as iitmResolve } from 'import-in-the-middle/hook.mjs'
async function resolve (specifier, context, parentResolve) {
if (!context.parentURL) {
const resolved = await parentResolve(specifier, context)
if (resolved.format === 'commonjs') {
return resolved
}
}
return iitmResolve(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
1
(as expected)npm run test:iitm
mocha
exits with0
(1
is expected)Specifications
import-in-the-middle@1.4.2
mocha@10.2.0