Skip to content
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

Open
pichlermarc opened this issue Oct 17, 2023 · 1 comment
Open

tests not running when using import-in-the-middle with mocha #34

pichlermarc opened this issue Oct 17, 2023 · 1 comment

Comments

@pichlermarc
Copy link

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.

Expected Behavior

mocha runs tests even if IITM is used

Actual Behavior

mocha exists with 0, not running any tests

Steps 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.

  1. git clone https://github.com/pichlermarc/esm-test
  2. npm install
  3. npm run test
    - this does not use the IITM hook, tests do run, and mocha exits with 1 (as expected)
  4. npm run test:iitm
    - this uses the IITM hook, tests do not run, and mocha exits with 0 (1 is expected)

Specifications

  • Version:
    • import-in-the-middle@1.4.2
    • mocha@10.2.0
    • Node.js v18.18.1
  • Platform: Ubuntu 22.04
  • Subsystem:
@bengl
Copy link
Member

bengl commented Dec 4, 2023

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.

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

No branches or pull requests

2 participants