hbenl / vscode-mocha-test-adapter

Mocha Test Adapter for the VS Code Test Explorer
MIT License
91 stars 31 forks source link

Explorer shows error with ESM and Mocha 9 because lib/esm-utils.js has moved #191

Closed dominique-pfister closed 2 years ago

dominique-pfister commented 2 years ago

I'm using Mocha Test Explorer 2.13.0, and open a ESM project that uses Mocha 9.2.0. The project has a require:

    "require": [
      "test/setup-env.js"
    ],

which is again an ESM.

I'm unable to see any test units in the explorer, and an error is logged:

[ERR_REQUIRE_ESM]: Must use import to load ES Module: [...]/test/setup-env.js
require() of ES modules is not supported.
require() of [...]/test/setup-env.js from ~/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.0/out/worker/bundle.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 setup-env.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from [...]/package.json.
    at new NodeError (internal/errors.js:322:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at ~/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.0/out/worker/bundle.js:5748:35
    at Generator.next (<anonymous>)
    at fulfilled (~/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.0/out/worker/bundle.js:115:58) {
  code: 'ERR_REQUIRE_ESM'

Looking in the explorer's bundle.js, I can see that it tries to locate a file lib/esm-utils.js in mocha, that exports a requireOrImport method:

https://github.com/hbenl/vscode-mocha-test-adapter/blob/8d694daea21d598d350f39df8b1423c6936d89f4/src/worker/main.ts#L80-L88

This file has moved in Mocha 9, though, and is now located in lib/nodejs/esm-utils.js. Changing the path in the bundle fixes the problem, so I suggest to check both paths in the bundle.

hbenl commented 2 years ago

Fixed in version 2.13.1. Thanks for figuring this out!