Run mocha reporter programatically

I want to use the test functionality in webstorm, but the only way to use it is trough a  mocha 'Run/Debug Configuration'.

 

The problem is that I have a `runner.ts` file and running mocha programatically using mocha.run() (https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically) and I can't run my tests in that way and have WebStorm tests and coverage funcionality.

 

I already tried using  web storm reporter 'mochaIntellijReporter.js' but don't have a way to integrate that with the IDE

 

Is it possible to use webstorm tests functionality in that case?

0

No, you need running mocha through the Mocha test runner to be able to use Mocha run configuration; with custom test runners, you can only run Mocha tests in IDE using Node.js run configuration

0
Avatar
Permanently deleted user

The functionality used by the IDE "only" listens to the output of the reporter `mochaIntellijReporter` that comes with webstorm `webstorm/90/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js`...

It would be great if we could call that listener on a *Node.js* configuration or something.. 

Does it make sense what I'm saying?

Thanks

0

Adding a listener on Node.js run/debug configuration that will switch to Mocha run/debug configuration is not supported and unlikely it will be done.

But you can create your custom compatible adapter for Mocha. Here is an example of such an adapter (`my-project/my-mocha-runner`) and mocha is installed as `my-project/node_modules/mocha`: 

// my-project/my-mocha-runner/bin/_mocha
#!/usr/bin/env node

require('../my-runner.js');

 

// my-project/my-mocha-runner/my-runner.js
const Mocha = require('mocha'),
fs = require('fs'),
path = require('path');

const mocha = new Mocha();

console.log(JSON.stringify(process.argv, null, 2)); // debug dump of args

const reporterInd = process.argv.indexOf("--reporter");
mocha.reporter(process.argv[reporterInd + 1]); // add Intellij reporter

mocha.addFile(process.argv[reporterInd + 2]); // assumed to be a test file

mocha.run(function(failures) {
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
});

 

Then, specify `/path/to/my-project/my-mocha-runner` as "Mocha package" in a Mocha run/debug configuration and run/debug it.

1
Avatar
Permanently deleted user

That worked for the tests! :D Thanks!

 

Only one final question, when I try to run  the "coverage" action of this configuration, I get:


`Error: spawn /<path_to_project>/tests/bin/_mocha EACCES`

 

Is there a way to get it to work without using sudo?

0

Yeah, `my-project/my-mocha-runner/bin/_mocha` needs execution permissions to run with coverage. For example, `chmod +x my-mocha-runner/bin/_mocha` for Linux.

0

请先登录再写评论。