tar jxvf jscoverage-0.5.1.tar.bz2
On compilation, jscoverage and jscoverage-server executables are created. Install them with below command
This file is used to execute the instrumented code. To view this file open it in your browser. It may happen your browser doesn’t allow access to it with file:// protocol. Host this file on any local server of your choice. For example, if you have an apache server running, place it in web folder and access it using http:// scheme. Or if you have chrome installed, open it with –allow-file-access-from-files option.
jscoverage.html file, has tabbed view containing Browser, Summary, Source and About tabs.
If you are facing errors while loading the url in iframe from Browser tab. Try clicking second option as Open in window. This will load your url in a new window.
JSCoverage also provides jscoverage-server program, which is a web server and can be used to host files from your code directory. Navigate to your code root directory and execute below command:
Once the server is running, access web interface from url http://127.0.0.1:8080/jscoverage.html This page will have an extra tab Store in addition to the ones generated by jscoverage program. Click Store Report button in this tab to get coverage data saved in jscoverage-report/ directory. To view this report file you don’t need jscoverage-server to be running. View jscoverage-report/jscoverage.html file from any of your web browser. To stop the server, execute the below command:
$ npm install --save-dev nyc
We will see how Istanbul uses nyc in testing framework Mocha. Open your npm package.json file and add below snippet:
"test": "nyc ava --timeout=3000"
Save the file. Now whenever the tests execute your code or any subprocess that it spawns, it will get instrumented.
By default report file is generated in text format. Get it in html by modifying above snippet as:
"test": "nyc --reporter=html --reporter=text mocha"
This will write report file to ./coverage/index.html file. Change –reporter value for getting a report in another format. AVA and Node Tap also uses nyc and you need to make similar changes as above to your package.json file. For tap, add snippet as below to get coverage file in HTML format
“test”: “tap –coverage –coverage-report=html”
For continuous-integration flows, integrate Istanbul with coveralls.io. Add coveralls dependency to module as
npm install coveralls --save-dev
Now update scripts in package.json file as
“test”: “nyc –reporter=html –reporter=text mocha”,
“coverage”: “nyc report –reporter=text-lcov | coveralls”
This will give you latest code-coverage statistics on all of your repositories including the total percentages covered and the lines covered.
Or run the command directly from command-line
npx intern coverage='_build/src/**/*.js'
Files present in the coverage list are instrumented at the time for loading. Intern has node based reporters, which output coverage summary to the console, in case coverage data is collected. Cobertura (‘cobertura’), html (‘htmlcoverage’), json (‘jsoncoverage’), and lcov (‘lcov’) Istanbul reporters are accessible from Intern. Just add reporters detail in config file
“reporters”: [ “runner”, “jsoncoverage” ]
Or run in command-line
npx intern reporters=runner reporters=jsoncoverage
File reports are written to coverage/* directory in the project root by default. You can specify alternate directory by adding below in your config file
This will generate file in location you specify in filename attribute.
In this post we learnt about code coverage tools like JSCoverage and Istanbul. JSCoverage offers features like line, branch, and function coverage without requiring a browser to run. Since it is written in Java, JSCoverage is platform independent and easy to setup.