npm module code coverage in 2 steps

npm module code coverage in 2 steps

We recently discovered the code coverage tool Istanbul, and were pleased to find that some of our small and well tested modules had 100% coverage. However, some modules had un-tested statements, lines, functions or branches. This made me mad! I set about adding coverage to my modules.

Following these two simple steps you can quickly and simply add code coverage reporting to your existing unit tests:

1. Install Istanbul as a dev dependency

You'll need to add Istanbul to your dev dependency list (not dependencies, because you don't want your consumers to have to download it when you install your module). You could manually edit your package.json, then npm install it (like a chump), or you could install it and update your package.json in one fell swoop:

npm install istanbul --save-dev

2. Amend your npm test script

Any self respecting module author lists their test command in their package.json. If you don't, you should feel ashamed and add it right now.

I'll take you through my new command:

"scripts": {
  "test":
    "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- -R spec"
}

node_modules/.bin/istanbul test runs the istanbul executable with the 'test' command.

node_modules/.bin/_mocha points to the mocha executable which is how my tests are run.

-R spec are my arguments for mocha.

-- tells istanbul to pass the following arguments the test process rather than consume them itself.

Mocha users, note the _mocha executable path contains an underscore. This is because the executable without an underscore forks processes to run your tests, but istanbul needs the tests in the same process. Other test framework usage may vary.

You can now run your tests with:

npm test

Istanbul just runs your tests for you without instrumenting.

And generate a coverage report with:

npm test --coverage

You will get some terminal output, with any luck 100% coverage like my module:

===== Coverage summary ======
Statements : 100% ( 64/64 )
Branches : 100% ( 30/30 )
Functions : 100% ( 13/13 )
Lines : 100% ( 58/58 )
=============================

…and an html coverage report at ./coverage

Optional extras:

If (like me) you don't want your coverage report cluttering up your repo, add coverage to your .gitignore

That's it. Happy coveraging!

Want to discuss a project?

Got a question? Want some more detail? We'd love to hear from you, contact Jason on +44 (0)1923 261166 or jason.treloar@clock.co.uk.

Related

databreach_istock__matej_moderc_thumb800.jpgRead
Article
26 May 2016

How to stop your customers' data being stolen

If we, as an industry, take anything from the data leaks at TalkTalk, British Gas and Morrisons, it should be that we must take every measure we can to secure customer data. Offering customers a more personalised experience means providing an environment where they are confident that the information they provide will be safe. Collecting and storing customer data and finding out more about your users is key to generating leads and gaining customer insight. But in the rush to get campaigns out the door and find affordable ways to create your digital products, ensuring third parties don’t risk your customers’ privacy and your reputations can be overlooked.

Screen Shot 2016-05-13 at 12.44.37.pngRead
Article
16 May 2016

How to build, test, share and publish a javascript Hybrid mobile application using Cordova

Mobile Applications (Apps) are something every developer wants to create, however, not every developer wants to have to learn multiple languages to be able to create an App which works across different types of devices, such as Android and iOS. Learning Objective C (or Swift) and Java is probably enough to put most people off the idea of creating a cross-platform App. However, it’s possible to create one using technologies which most developers are familiar with. Good old HTML, CSS and JavaScript is all you need. Well, that and Apache Cordova, the mobile application development framework that allows you to build Apps for multiple platforms using a single code base.

Blog-post-img-01.jpgRead
Article
26 April 2016

MongoDB Performance on ZFS and Linux

Here at Clock we love ZFS, and have been running it in production on our Linux file servers for several years. It provides us with numerous excellent features. With the recent release of Ubuntu Xenial 16.04 official support for ZFS is now here, and we are keen to integrate it fully into our next generation hosting stack.

node blog post.jpgRead
Article
9 March 2016

A Simple Website in Node.js – 2016 Edition

Four years ago I wrote a post on how to build a simple website in Node.js. Seeing as it’s still the most popular article on this blog, I thought I’d take a look at how things have evolved, what I would do differently today, and some additional nuggets of advice.

Come and work for Clock

Clock is made up of bright, hard-working and talented people and we're always on the look out for more. You can browse the current jobs below or follow us @clock for the latest vacancies.

View Latest
Vacancies