22

I've recently started getting into unit testing for my Node projects with the help of Mocha. Things are going great so far and I've found that my code has improved significantly now that I'm thinking about all the angles to cover in my tests.

Now, I'd like to share my experience with the rest of my team and get them going with their own tests. Part of the information I'd like to share is how much of my code is actually covered.

Below is a sample of my application structure which I've separated into different components, or modules. In order to promote reuse I'm trying to keep all dependencies to a minimum and isolated to the component folder. This includes keeping tests isolated as well instead of the default test/ folder in the project root.

| app/
| - component/
| -- index.js
| -- test/
| ---- index.js

Currently my package.json looks like this. I'm toying around with Istanbul, but I'm in no way tied to it. I have also tried using Blanket with similar levels of success.

{
  "scripts": {
    "test": "clear && mocha app/ app/**/test/*.js",
    "test-cov": "clear && istanbul cover npm test"
}

If I run my test-cov command as it is, I get the following error from Istanbul (which is not helpful):

No coverage information was collected, exit without writing coverage information

So my question would be this: Given my current application structure and environment, how can I correctly report on my code coverage using Istanbul (or another tool)?


TL;DR

How can I report on my code coverage using Node, Mocha, and my current application structure?


EDIT

To be clear, Mocha is running tests correctly in this current state. Getting the code coverage report is what I'm struggling with getting to work.

EDIT 2

I received a notification that another question may have answered my question already. It only suggested installing Istanbul and running the cover command, which I have done already. Another suggestion recommends running the test commands with _mocha, which from some research I have done is to prevent Istanbul from swallowing the flags meant for Mocha and is not necessary in newer versions of Mocha.

Community
  • 1
  • 1
Chris Wright
  • 754
  • 1
  • 8
  • 19
  • Duplicate question, look at http://stackoverflow.com/questions/16633246/code-coverage-with-mocha for more info. – ivoszz May 27 '15 at 18:45
  • Thanks for taking the time to post that. I came across that in my research before posting this question and it did not help. The answer only suggests installing Istanbul and running the cover command, and as you can see I've already done that. – Chris Wright May 27 '15 at 18:52
  • It also suggests using _mocha instead of whatever you are using. – Kevin B May 27 '15 at 18:56
  • You're right. I've read about why that suggestion was made (having to do with parameters being eaten up by Istanbul), and also that it's not necessary in newer versions of Mocha. Either way, the result is the same. Thanks for your time. – Chris Wright May 27 '15 at 19:01
  • Were you able to get this working @ChrisWright ? – djv Dec 20 '15 at 20:39
  • @damionjn Unfortunately not. When I do I'll be sure to update this question. – Chris Wright Dec 21 '15 at 00:33

3 Answers3

6

You should try running your test like this :

istanbul cover _mocha test/**/*.js
Sachacr
  • 704
  • 1
  • 9
  • 17
1

You need an .istanbul.yml file. I don't see any reference to it - hard to say without knowing the contents of it. I don't think there's quite enough information to solve this in the question. I'll update this answer if you update the question, especially before the bounty expires, eh?

Jeremy Anderson
  • 826
  • 5
  • 16
  • The question is simply this: how can I get coverage results when my tests are in different subfolders instead of the default `test/` folder. I don't currently have this file, so the question has all the details available. If I can use that config file to accomplish this (I've done some searching since you posted and found nothing), please let me know how and I'll choose your answer and award you the bounty. – Chris Wright Jun 07 '15 at 15:28
  • I was pretty sure you could define the test directories as well as the ones you want to cover inside that file, but I'm out of time at the moment, no worry here. Best of luck, Chris. – Jeremy Anderson Jun 08 '15 at 12:33
0

This is how i get code coverage on all my js projects (looks like the one from Sachacr) :

istanbul cover _mocha -- -R spec --recursive test
Pierre Inglebert
  • 3,858
  • 1
  • 18
  • 22