vscode-mocha-test-adapter icon indicating copy to clipboard operation
vscode-mocha-test-adapter copied to clipboard

Cannot implement NYC or NYC wrapper for ESM written tests

Open robertdumitrescu opened this issue 4 years ago • 2 comments

Hey I tried calling mocha in v2.3.0 with the nyc argument and is not even running. I click the button and nothing happens. And then I tried this: #49 and I am getting the following error:

[2020-04-06 14:10:18.883] [INFO] Running test(s) ["c:\\Users\\rober\\gitRepos\\royal-fiber\\src\\css.Helper.Spec.js: CssHelper -> percentify"] of c:\Users\rober\gitRepos\royal-fiber
[2020-04-06 14:10:18.957] [INFO] Worker: Using the mocha package at c:\Users\rober\gitRepos\royal-fiber\mocha_nyc.cjs
[2020-04-06 14:10:19.037] [INFO] Worker: Loading files
[2020-04-06 14:10:19.037] [INFO] Worker: Trying to use Mocha's experimental ESM module loader
[2020-04-06 14:10:19.262] [INFO] Worker: Caught error Error: Cannot find module 'c:\Users\rober\gitRepos\royal-fiber\mocha_nyc.cjs/lib/utils'
Require stack:
- c:\Users\rober\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.3.0\out\worker\bundle.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:976:15)
    at Function.Module._load (internal/modules/cjs/loader.js:859:27)
    at Module.require (internal/modules/cjs/loader.js:1036:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at c:\Users\rober\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.3.0\out\worker\bundle.js:1762:35
    at Generator.next (<anonymous>)
    at fulfilled (c:\Users\rober\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.3.0\out\worker\bundle.js:82:58) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'c:\\Users\\rober\\.vscode\\extensions\\hbenl.vscode-mocha-test-adapter-2.3.0\\out\\worker\\bundle.js'
  ]
}
[2020-04-06 14:10:19.271] [INFO] Worker finished

The wrapper I ended up using in mocha_nyc.cjs is like following:

// const Mocha = require('mocha');

// import Mocha from "mocha";
const Mocha = require('mocha');
// const Nyc = require('nyc');

const FakeMocha = function FakeMocha(...args) {
//   const nyc = new Nyc({
//     reporter: [ 'lcovonly' ]
//   });
//   nyc.reset();
//   nyc.wrap();

  Object.setPrototypeOf(this, Mocha.prototype);
  Mocha.call(this, ...args);

  const origRun = this.run;
  this.run = (cb) => {
    const runner = origRun.call(this, cb);
    runner.on('end', () => {

      console.log('bla');
    //   nyc.writeCoverageFile();

    //   nyc.report();
    });
    return runner;
  };

  return this;
};
Object.setPrototypeOf(FakeMocha, Mocha);

// export default FakeMocha;
module.exports = FakeMocha;

Nyc is commented out since I wanted to see the tests running at least and deal with coverage later. It would be amazing if this plugin would offer coverage out of the box. Is definitely reaching the maturity point since is supporting so many configurations and test variations.

robertdumitrescu avatar Apr 06 '20 14:04 robertdumitrescu

I have just created a launcher script for using Mocha Test Explorer with nyc. To use it, add the launcher scripts to your project:

npm install --save-dev mocha-explorer-launcher-scripts

Then add the following to your VS Code settings:

"mochaExplorer.launcherScript": "node_modules/mocha-explorer-launcher-scripts/nyc"

I'll add some documentation about the options later, but it should work without any additional configuration.

hbenl avatar Jun 01 '20 16:06 hbenl

Cool, nice one. Thank you for your support. Greatly appreciated.

robertdumitrescu avatar Jun 05 '20 11:06 robertdumitrescu