0

I'm trying to use Istanbul along with Mocha in Node.js to run unit tests and generate code coverage reports. I'm using the following code to run run the unit tests and generate those code reports.

istanbul cover _mocha -- -R tap 'test/*.test.js' > test.tap; istanbul report clover

If I just want to run unit tests without code coverage reports I can just run the following.

mocha

Both of these methods work fine. But the first method doesn't really print anything to the console. I have no idea which unit test it is currently running and when it's all complete I have no idea what exactly went wrong. It doesn't provide any form of error logs or anything. The second method prints the status of that certain unit test after each test so it's easy in the console to see exactly what unit test you are currently working on and after all tests are complete it gives you details and error logs about why they failed so you can start to debug. The first method doesn't provide any of this.

Is there anyway to generate code coverage reports using Istanbul but have it print all the details to the console that just running mocha prints? If so how can I achieve this? Some of my tests take a little bit to run and finish so when generating code coverage reports with Istanbul it would be nice to see exactly what test it's currently on and more detail about the test in real time.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179

2 Answers2

1

Your istanbul command redirects the output to test.tap, so you are not going to see it on the console:

istanbul cover _mocha -- -R tap 'test/*.test.js' > test.tap; istanbul report clover
                                                 ^^^^^^^^^^

The test output in test.tap is not needed by Istanbul, so you could remove the redirection. If you want the command to continue writing the file, you could use the tee command to write the test output to both the file and the console.

Community
  • 1
  • 1
cartant
  • 57,105
  • 17
  • 163
  • 197
  • Awesome! Thanks so much. Changing the code to `istanbul cover _mocha -- -R tap 'test/*.test.js' | tee test.tap; istanbul report clover` seems to work. Still doesn't seem like it gives as much detail as just running `mocha` does sadly. Wish it outputted to the console in the format `mocha` does. Oh well. Thanks again. – Charlie Fish Aug 10 '16 at 00:31
  • You don't have to use the `tap` reporter. Just leave out the `-R tap` part and you should get pretty much the same output as with the `mocha` command. – cartant Aug 10 '16 at 00:33
  • I'm using some strange Jenkins plugins I don't totally understand. I'm pretty sure they require the tap file tho. – Charlie Fish Aug 10 '16 at 00:33
1

Try this: https://github.com/glenjamin/mocha-multi

mocha-multi keeps changing the value of process.stdout and process.stderr whenever a reporter is doing its thing.

Run this: istanbul cover _mocha -- --recursive --reporter mocha-multi --reporter-options list=-,tap=test.tap 'test/*.test.js' && istanbul report clover

Jane
  • 21
  • 1