Summary: you can get code coverage stats for your node.js code
(FYI my dev box uses Ubuntu Linux 13.10.)
I’ve been developing new features in my node.js application, and concurrently writing vows.js tests for the new code. Yesterday, I decided that it was about time that I check to see just how much code my tests are covering.
I looked at code coverage in npm and saw a number of potentially useful libraries, but didn’t want to research each one to see if they worked with vows. Then I noticed that the vows page says “Code coverage reporting is available if instrumented code is detected. Currently only instrumentation via node-jscoverage is supported.” Well, that makes it a no-brainer. They give you instructions for downloading and installing node-jscoverage, which is not in npm!
While I was hunting around, I had found Alex Seville’s post about some problems he had encountered using node-jscoverage. That made me concerned that the instructions on the vows page were incomplete. So I followed his directions. In his step 2, I ran into a little permission trouble; I had to run each command as sudoer:
1 2 3
From then on, almost everything worked. Here’s my node.js app code:
1 2 3
And here’s how I produced the instrumented code to test:
Notice I put my instrumented code into a directory which is completely independent from my app directory structure. You don’t want instrumented code running in your app! It’s only used for generating coverage reports.
I followed Alex’s link to Jeff Kunkle’s post about switching between testing instrumented and non-instrumented code. I grabbed Jeff’s sample code to do this, and it worked like a charm. Then I ran a test –
1 2 3 4 5 6 7 8 9 10 11 12
Oops! The instrumented code couldn’t find my app’s libraries (such as moment) in
/srv/app/node_modules. So I created a soft link to these in my
After doing that, my instrumented code appeared to run fine; I got the usual happy output from vows:
1 2 3
But how could I be sure that the instrumented code was being exercised? This time, I reran my test with the
Sure enough, I saw a file called
coverage.html had been generated in my current directory. I opened it in my browser window to view an html display of what code is covered, and what still needs to be tested. Nice!