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

Import error in ESM *.spec.js file results in commonjs loading to be used #4697

Closed
4 tasks done
TheSlowGrowth opened this issue Jul 22, 2021 · 1 comment
Closed
4 tasks done
Labels
duplicate been there, done that, got the t-shirt...

Comments

@TheSlowGrowth
Copy link

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

  • Assume a test file with a broken import statement, e.g. due to the requested package not being installed.
  • The containing package is ESM, so package.json: "type": "module"
  • this results in very confusing errors: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: myTestFile.spec.js

Steps to Reproduce

  • Assume a test file with a broken import statement, e.g. due to the requested package not being installed.
  • The containing package is ESM, so package.json: "type": "module"
  • Mocha tries to import the file via import, due to mocha/lib/esm-utils.js:42
exports.requireOrImport = hasStableEsmImplementation
  ? async file => {
      if (path.extname(file) === '.mjs') {
        return formattedImport(file);
      }
      try {
        return dealWithExports(await formattedImport(file));
      } catch (err) {
        if (
          err.code === 'ERR_MODULE_NOT_FOUND' ||
          err.code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
          err.code === 'ERR_UNSUPPORTED_DIR_IMPORT'
        ) {
          return require(file);
        } else {
          throw err;
        }
      }
    }
  : implementationOfRequireOrImportForUnstableEsm;
  • If the loaded file has a problem in its imports, the catch statement catches the error and tries to load the file as a commonjs module.
  • this results in very confusing errors: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: myTestFile.spec.js

Expected behavior: The import error from the test file is displayed so I can diagnose it (especially important for CI where I can't easily reproduce the error locally)

Actual behavior: The import error from the test file is hidden, instead the test file gets imported with require() resulting in another error message that has nothing to do with the actual problem

Reproduces how often: 100%

Versions

  • The output of mocha --version: command not found
  • The output of node node_modules/.bin/mocha --version: 9.0.2
  • The output of node --version: 16.5.0
  • Your operating system
    • name and version: OSX 10.15.7 & ubuntu20.04
    • architecture (32 or 64-bit): 64bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): zsh
  • Any third-party Mocha-related modules (and their versions): none
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): typescript, but replicates with *.js files as well
@juergba juergba added duplicate been there, done that, got the t-shirt... and removed unconfirmed-bug labels Jul 22, 2021
@juergba
Copy link
Member

juergba commented Jul 22, 2021

duplicate of #4675.
This issue is already fixed and will be published with our next release.

@juergba juergba closed this as completed Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate been there, done that, got the t-shirt...
Projects
None yet
Development

No branches or pull requests

2 participants