-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests not running when using import-in-the-middle
with mocha
#34
Comments
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
- 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
import-in-the-middle@1.4.2
mocha@10.2.0
The text was updated successfully, but these errors were encountered: