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.

6
  • Duplicate question, look at stackoverflow.com/questions/16633246/code-coverage-with-mocha for more info. Commented May 27, 2015 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. Commented May 27, 2015 at 18:52
  • It also suggests using _mocha instead of whatever you are using. Commented May 27, 2015 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. Commented May 27, 2015 at 19:01
  • Were you able to get this working @ChrisWright ? Commented Dec 20, 2015 at 20:39

3 Answers 3

6
+25

You should try running your test like this :

istanbul cover _mocha test/**/*.js
Sign up to request clarification or add additional context in comments.

Comments

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?

2 Comments

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.
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.