npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

learn-istanbul

v2.0.0

Published

Learn how to use istanbul for tracking test/code coverage in your JS projects

Downloads

22

Readme

# Learn Istanbul

Build Status codecov.io devDependency Status

Learn how to use Istanbul to check/track Code Coverage in your JavaScript projects.


Sign Not In Use

Why?

Like the road sign that is "Not In Use" too much code being written never gets executed.

There are a few obvious issues with this:

  1. if un-tested code remains in the codebase it can contain unknown behaviour e.g. bugs.
  2. untested features are more difficult to maintain without introducing breaking changes.
  3. un-tested code can clutter a project and accumulates technical debt that wastes time.

What?

Code coverage tells you when code you have written is being executed so you can decide if un-covered lines are superfluous (and can be removed) or require additional testing.

The rest of this page will focus on practical usage example, so if you are completely new to Code Coverage we recommend you read the wikipedia article: http://en.wikipedia.org/wiki/Code_coverage first.

Istanbul is a code coverage analysis script you run when executing your unit tests: https://github.com/gotwarlost/istanbul/ we like it because it's simple and prints out nice html reports (see below)

How?

Installation

We prefer to install istanbul as a "devDependencies" in each of our projects:

npm install istanbul --save-dev

to check if the installation worked, (copy-paste and) run the following command in your terminal:

node_modules/.bin/istanbul help

Simple Example

For our first example create a file called test.js.

vi test.js

type (or copy-paste) the following code in the test.js file:

x = 42;
if(false)
     x =-1;

Now run the istanbul command to generate a coverage report:

node ./node_modules/.bin/istanbul cover test.js

Alternatively you can insert the line

"coverage": "node ./node_modules/.bin/istanbul cover test.js"

into the scripts section of your package.json and run

npm run coverage

This will create a directory in your project called coverage where you will find the generated coverage reports. In our case: learn-istanbul/coverage/lcov-report/learning-istanbul/test1.js.html If you open the test1.js**.html** file in your browser you will see a visual coverage report:

Basic coverage report

Istanbul gives us four code coverage metrics:

  • Statements: How many of the statements in you code are executed.
  • Branches: Conditional statements create branches of code which may not be executed (e.g. if/else). This metric tells you how many of your branches have been executed.
  • Functions: The proportion of the functions you have defined which have been called.
  • Lines: The proportion of lines of code which have been executed.

when you click test.js to view the coverage for the file you see:

learn-istanbul-test js_html

Two things to note in the example above:

  • we only get 66.67% coverage because the only 2/3 of the code is being run
  • the 3rd line never gets executed because false is always false!

This may be a trivial example but it shows exactly where the useless code is.

#### A more "Real World" Example

Try executing the mischief.js file by running npm test:

learn-istanbul-terminal-run

What is wrong with the following picture?

96 % Code Coverage

There are plenty of developers/organisations that can only dream about getting 96% code coverage! and yet when we inspect the detail, there's something big slipping through the net!

learn-istanbul-mischief-on-line-34

We have 100% functional code coverage, but only 50% "Branch" Coverage. This means one or more conditional execution branches is not being executed.

Most of the time it will be something innocuous but what if a disgruntled person slipped in something like:

if(employee.status === 'terminated' && employee.left - today() > 90) {
	selfDestuct();
}

The 97% Coverage is not looking so hot anymore ...

What if we add a Test that follows the branch containing the rogue code? We reach our mythical 100% Coverage:

learn-istanbul-mischief-100-percent

And if we simply allow this code to be promoted without further checks, the rogue code will be in production and soon forgotten.

100 % Code Coverage includes Rogue Code

The solution here is to not rely (solely) on tools such as Istanbul to check code. Its essential would advocate a separation between the people writing the tests and the developers who write the code.

And there is still no substitute for Code Review!

87% Test Coverage

Notes

  • Ariay's basic tutorial: http://ariya.ofilabs.com/2012/12/javascript-code-coverage-with-istanbul.html
  • Jasmine Test Coverage: http://architects.dzone.com/articles/code-coverage-jasmine-tests

Istanbul (the JavaScript Code Coverage tool) https://github.com/gotwarlost/istanbul should not to be confused with istanbul the desktop screen recorder, they are totally diferent animals! Shame about the name collision... :-(

HitCount Join the chat at https://gitter.im/dwyl/chat