mocha + webstorm - error message broken

2019-02-20 16:17发布

问题:

I try to use mocha with webstorm test runner on win 7.

I have these config params:

  • path to node:

    C:\Program Files (x86)\nodejs\node.exe
    

    working directory:

    D:\creation\software developer\projects\dev\document root\
    

    path to node app js file:

    C:\Users\inf3rno\node_modules\mocha\bin\_mocha
    

    application parameters:

    test
    

I got the following error message in webstorm console

✖ 1 of 1 test failed:

but nothing about which test failed. :S

How to fix it?

(in git bash I got a detailed error message, so mocha is installed well)

Tried with different reporters, but none of them seems working. The problem is with the Error object I think. Everything else is displayed well.

Wrote a custom reporter and found that it's fully random whether the stack shows up on the webstorm console, or not. The best choice was to print it with process.stderr.write, with console.log or process.stdout.write appears nothing. I don't know why is that. Maybe it's some type of timeout, but I set the timeout to 9999999. :S :S :S Hmm maybe the webstorm runner has a timeout, and it depends on that, not on the mocha timeout settings...

Tested with this reporter:

exports = module.exports = WebStorm;

/**
 * Initialize a new `WebStorm` test reporter.
 *
 * @param {Runner} runner
 * @api public
 */

 //decribe -> suite
 //it -> test

function WebStorm(runner) {
  var buffer = [];
  var suites = [];
  var fails = [];

  runner.on("suite", function(suite){
     suites.push(suite.title || "''");
  });

  runner.on("suite end", function(suite){
     if (fails.length > 0)
         buffer.push("describe "+suites.join(".")+"\n"+fails.join("\n"));
     fails = [];
     suites.pop();
  });

  runner.on("fail", function(test, err){
      fails.push("  it fail "+test.fullTitle() + err.message);
  });

  runner.on("end", function (){
      process.stderr.write(buffer.length+"\n");
      //process.stderr.write(buffer.join("\n"));
  });
}

I found it does not really matter how long the output text is, just how long you have to wait before displaying it. So I still think it's a timeout problem, and not an stdout bug. The webstorm console returns: "Process finished with exit code 59" which means "An unexpected network error occured."

Changed to jasmine-node, it's working.

Edit:

I changed to jasmine2, it had the same issue on windows. The simplest workaround is deferring process.exit for example with 1ms. So it cannot exit before the end of the stdout.write. (Manually flushing the stdout and calling exit only after that is probably a working solution as well.)

回答1:

It's caused by a known Windows specific node issue, also logged in JetBrains issue tracker.



回答2:

While the greater powers are debating on the hows and the whys to fix this, lesser mortals can resort to this workaround:

  • Create a file called testRunner.js (or whatever)
  • add the following lines of code to it
  • modify the list of test files per your project
  • in webstorm, run the testRunner.js to run your tests

testRunner.js

var testFiles=["test/_helper.js","test/tests.js","test/tests.coffee"];

var Mocha = require('mocha');

var mocha = new Mocha;

mocha.reporter('spec').ui('bdd');

for (var i =0;i<testFiles.length;i++){
 mocha.addFile(testFiles[i]);
}

var runner = mocha.run(function(){
                console.log('finished');

});

Courtesy: http://wuntusk.blogspot.com/2012/06/using-webstorm-or-phpstorm-and-mocha.html



回答3:

Even after implementing Mrchief's answer, I am still getting scrambled reports in the output. I ended up running tests in the terminal window. As a reference;

Make sure mocha is installed globally.

npm install -g mocha

Open the terminal window (Alt-Minus) and change to the directory where your test scripts are

Run mocha

mocha -R spec *.js