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

Parameter --require cannot load an ES module. #4281

Closed
3 of 4 tasks
regseb opened this issue May 11, 2020 · 2 comments · Fixed by #4304
Closed
3 of 4 tasks

Parameter --require cannot load an ES module. #4281

regseb opened this issue May 11, 2020 · 2 comments · Fixed by #4304
Labels
type: feature enhancement proposal

Comments

@regseb
Copy link

regseb commented May 11, 2020

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

Parameter --require cannot load an ES module.

Steps to Reproduce

package.json:

{
    "name": "testcase",
    "version": "1.0.0",
    "type": "module",
    "scripts": {
        "test": "mocha test.js"
    },
    "dependencies": {
        "mocha": "^7.1.2"
    },
    "mocha": {
        "require": "require.js"
    }
}

require.js:

// An ES module because { "type": "module" } in package.json.

test.js:

import assert from "assert";

describe("foo", function () {
    it("bar", function () {
        assert.ok("baz");
    });
});
  • unzip testcase.zip;
  • npm install
  • npm test

Expected behavior: [What you expect to happen]

> testcase@1.0.0 test /home/regseb/testcase
> mocha test.js



  foo
    ✓ bar


  1 passing (6ms)

Actual behavior: [What actually happens]

> testcase@1.0.0 test /home/regseb/testcase
> mocha test.js

mocha inspect [spec..]

Run tests with Mocha

Rules & Behavior
  --allow-uncaught           Allow uncaught errors to propagate        [boolean]
[...]
  --list-reporters   List built-in reporters & exit                    [boolean]

✖ ERROR: Must use import to load ES Module: /home/regseb/testcase/require.js
require() of ES modules is not supported.
require() of /home/regseb/testcase/require.js from /home/regseb/testcase/node_modules/mocha/lib/cli/run-helpers.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename require.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/regseb/testcase/package.json.

npm ERR! Test failed.  See above for more details.

Reproduces how often: [What percentage of the time does it reproduce?] 100%

Versions

  • The output of mocha --version: zsh: command not found: mocha
  • The output of node node_modules/.bin/mocha --version: 7.1.2
  • The output of node --version: v13.14.0
  • Your operating system
    • name and version: Ubuntu 19.10
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): zsh 5.7.1 (x86_64-ubuntu-linux-gnu)
  • Your browser and version (if running browser tests): N.A.
  • Any third-party Mocha-related modules (and their versions): no
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): no

Additional Information

Fix proposal: replace require by import. But dynamic import has been added to Node 13.2.0.

@boneskull boneskull added type: feature enhancement proposal and removed unconfirmed-bug labels May 18, 2020
@boneskull
Copy link
Contributor

This is known, and is a "missing feature"

@JacobLey
Copy link
Contributor

JacobLey commented May 27, 2020

Ran into this issue as well.

In a quick dive through the code, this seems like a feasible and quick tweak to support. I am happy to take a swing at a PR, but I wanted to discuss possible reasons not to support this, or alternative solutions.

The error spawns here: https://github.com/mochajs/mocha/blob/master/lib/cli/run-helpers.js#L92 See how the require method is being used directly.

If we swapped that require with the existing requireOrImport it should auto-magically support ESM.

Of course that makes the "require" async, but considering the handleRequires already has an async signature, there would not need to be any change to that methods usage. Would just need to convert that .reduce() into something async friendly (for ... of?).

JacobLey added a commit to JacobLey/mocha that referenced this issue May 27, 2020
Allow files/modules specified in `--require` to be ESM.

CommonJS loading is still supported and the default.
boneskull pushed a commit that referenced this issue Jun 1, 2020
* Support --require of ESM; closes #4281
Allow files/modules specified in `--require` to be ESM.

CommonJS loading is still supported and the default.

* Conditionally generate url for import
Windows compatible

* Add tests for --require ESM
As both .mjs and type=module (combined with cjs for good measure).

Updated linter to allow tests to use spread operator (ecmaVersion 2018)

Allow --require'd module to be an object, or "module"

* Revert change to eslintrc, use mocha to pass experimental flag

* Replace type() -> typeof
Add truthy check to handle null edge case

type(ES Module) => "module", but we treat it the same as an object

* Remove doc limitation for --require ESM

* Add note to --require docs about ESM support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature enhancement proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants