Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 962 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!

Existing egghead members will not see this. Sign in.

Adding code coverage recording with Istanbul

2:57 JavaScript lesson by

This lesson will utilize the Istanbul tool to instrument our code for code coverage recording and reporting. We'll use this to make sure that as our library gets new features, we keep track of our coverage and can find places to improve it.

Get the Code Now
click to level up

egghead.io comment guidelines


This lesson will utilize the Istanbul tool to instrument our code for code coverage recording and reporting. We'll use this to make sure that as our library gets new features, we keep track of our coverage and can find places to improve it.


Are there some further markup requirements to get Istanbul running? I am wondering this as I am attempting to work with jasmine

In reply to egghead.io
Kent C.

I've never set up coverage with jasmine, but I assume it's fairly straightforward. A quick google search turned up this. You may also be interested in checking out nyc :-)

In reply to steve

It was not as straightforward on windows as it should have been. Ran into a couple of issues. So I switched to mocha. Thanks for nyc. I will check it out. In the case that you have split all of your tests up into different directories could you reference it like so... istanbul cover -x.test.js _mocha -- -R spec tests ? Tests is my outermost directory.

In reply to Kent C.
Kent C.

I believe that should work. Actually, I think that test is the default directory. So if you name the directory test then you don't need to even provide a directory argument and could simply do: istanbul cover -x.test.js _mocha -- -R spec

Good luck!

In reply to steve

If you are getting:
error: No coverage information was collected, exit without writing coverage information
when running istanbul, try to install istanbul@1.0.0-alpha.2 instead, as was suggested here: https://github.com/gotwarlost/istanbul/issues/262

To add coverage to our library, we are going to use a tool called "Istanbul," which will instrument our code so that coverage can be recorded and reported. Install as a devDependency Istanbul. Then, we'll look at our package.json.

We'll see that Istanbul has been added to our devDependencies, and we need to make some alterations to our test scripts so that Istanbul can instrument our code for coverage reporting.

We'll do our test single one here first. What we're going to change is first we need to run the Istanbul command instead of the Mocha command. We'll say, "Istanbul cover," and then, we want to exclude our index.test.js, because that's a test file. We don't want to report coverage stats on that.

We'll exclude anything that ends in .test.js. Then, we're going to actually run the underscore Mocha command. This is in our bin as underscore Mocha. This is what Istanbul needs us to run for us to get coverage using the Mocha framework.

Now, we need to pass some arguments to the Mocha command, such as the file that we want to pass. We do this by specifying this double dash. Then, everything after here will be passed down into the binary that we're using to run our test.

We're passing our source test file. To get the coverage stats, we need to change our reporter to be spec. We'll save that, and then, we'll npm run test single.

This will run our test. We'll get the output in our console the same, but in addition, we'll get this coverage summary. We have 100 percent coverage. Let's go ahead and take a look at this file that was created, as well.

There's a coverage directory. It has a coverage.json and lcov.info. It also has this LCOV report that we can open in our browser. Let's go ahead and open that. We can browse our files, our source code, and see how many times each line was hit in our test.

It looks like we hit this line three times, and that makes sense, because we call that function one time with a number. That number just happens to be three. It iterates over that function three times.

In review, you simply install Istanbul. Then you say, "Istanbul cover." You exclude your test files, so you only cover your actual source. You change the Mocha to a underscore Mocha. Then, you add the RSpec reporter as an argument to the underscore Mocha binary.

One final thing that we want to make sure that we do is we don't want to commit this coverage folder to our source code. If we look at the get status, we'll see coverage is there but it's not a tracked file.

We want to make sure that it's not ever tracked. We'll go to our get ignore, and we'll simply add coverage in here so that that directory is ignored. If we look at the get status, we see that only the change files were...

Joel's Head
Why are we asking?