1 | ## Istanbul - a JS code coverage tool written in JS
|
2 |
|
3 | [![Build Status](https://secure.travis-ci.org/gotwarlost/istanbul.png)](http://travis-ci.org/gotwarlost/istanbul)
|
4 | [![Dependency Status](https://gemnasium.com/gotwarlost/istanbul.png)](https://gemnasium.com/gotwarlost/istanbul)
|
5 | [![Coverage Status](https://img.shields.io/coveralls/gotwarlost/istanbul.svg)](https://coveralls.io/r/gotwarlost/istanbul?branch=master)
|
6 |
|
7 | [![NPM](https://nodei.co/npm/istanbul.png?downloads=true)](https://nodei.co/npm/istanbul/)
|
8 |
|
9 | * [Features and use cases](#features)
|
10 | * [Getting started and configuration](#getting-started)
|
11 | * [The command line](#the-command-line)
|
12 | * [Ignoring code for coverage](#ignoring-code-for-coverage)
|
13 | * [API](#api)
|
14 | * [Changelog](https://github.com/gotwarlost/istanbul/blob/master/CHANGELOG.md)
|
15 | * [License and credits](#license)
|
16 |
|
17 | ### Features
|
18 |
|
19 | * All-javascript instrumentation library that tracks **statement, branch,
|
20 | and function coverage**.
|
21 | * **Module loader hooks** to instrument code on the fly
|
22 | * **Command line tools** to run node unit tests "with coverage turned on" and no cooperation
|
23 | whatsoever from the test runner
|
24 | * Multiple report formats: **HTML**, **LCOV**, **Cobertura** and more.
|
25 | * Ability to use as [middleware](https://github.com/gotwarlost/istanbul-middleware) when serving JS files that need to be tested on the browser.
|
26 | * Can be used on the **command line** as well as a **library**
|
27 | * Based on the awesome `esprima` parser and the equally awesome `escodegen` code generator
|
28 | * Well-tested on node (prev, current and next versions) and the browser (instrumentation library only)
|
29 |
|
30 | ### Use cases
|
31 |
|
32 | Supports the following use cases and more
|
33 |
|
34 | * transparent coverage of nodejs unit tests
|
35 | * instrumentation/ reporting of files in batch mode for browser tests
|
36 | * Server side code coverage for nodejs by embedding it as [custom middleware](https://github.com/gotwarlost/istanbul-middleware)
|
37 |
|
38 | ### Getting started
|
39 |
|
40 | $ npm install -g istanbul
|
41 |
|
42 | The best way to see it in action is to run node unit tests. Say you have a test
|
43 | script `test.js` that runs all tests for your node project without coverage.
|
44 |
|
45 | Simply:
|
46 |
|
47 | $ cd /path/to/your/source/root
|
48 | $ istanbul cover test.js
|
49 |
|
50 | and this should produce a `coverage.json`, `lcov.info` and `lcov-report/*html` under `./coverage`
|
51 |
|
52 | Sample of code coverage reports produced by this tool (for this tool!):
|
53 |
|
54 | [HTML reports](http://gotwarlost.github.com/istanbul/public/coverage/lcov-report/index.html)
|
55 |
|
56 |
|
57 | ### Configuring
|
58 |
|
59 | Drop a `.istanbul.yml` file at the top of the source tree to configure istanbul.
|
60 | `istanbul help config` tells you more about the config file format.
|
61 |
|
62 | ### The command line
|
63 |
|
64 | $ istanbul help
|
65 |
|
66 | gives you detailed help on all commands.
|
67 |
|
68 | ```
|
69 | Usage: istanbul help config | <command>
|
70 |
|
71 | `config` provides help with istanbul configuration
|
72 |
|
73 | Available commands are:
|
74 |
|
75 | check-coverage
|
76 | checks overall coverage against thresholds from coverage JSON
|
77 | files. Exits 1 if thresholds are not met, 0 otherwise
|
78 |
|
79 |
|
80 | cover transparently adds coverage information to a node command. Saves
|
81 | coverage.json and reports at the end of execution
|
82 |
|
83 |
|
84 | help shows help
|
85 |
|
86 |
|
87 | instrument
|
88 | instruments a file or a directory tree and writes the
|
89 | instrumented code to the desired output location
|
90 |
|
91 |
|
92 | report writes reports for coverage JSON objects produced in a previous
|
93 | run
|
94 |
|
95 |
|
96 | test cover a node command only when npm_config_coverage is set. Use in
|
97 | an `npm test` script for conditional coverage
|
98 |
|
99 |
|
100 | Command names can be abbreviated as long as the abbreviation is unambiguous
|
101 | ```
|
102 |
|
103 | #### The `cover` command
|
104 |
|
105 | $ istanbul cover my-test-script.js -- my test args
|
106 | # note the -- between the command name and the arguments to be passed
|
107 |
|
108 | The `cover` command can be used to get a coverage object and reports for any arbitrary
|
109 | node script. By default, coverage information is written under `./coverage` - this
|
110 | can be changed using command-line options.
|
111 |
|
112 | The `cover` command can also be passed an optional `--handle-sigint` flag to
|
113 | enable writing reports when a user triggers a manual SIGINT of the process that is
|
114 | being covered. This can be useful when you are generating coverage for a long lived process.
|
115 |
|
116 | #### The `test` command
|
117 |
|
118 | The `test` command has almost the same behavior as the `cover` command, except that
|
119 | it skips coverage unless the `npm_config_coverage` environment variable is set.
|
120 |
|
121 | **This command is deprecated** since the latest versions of npm do not seem to
|
122 | set the `npm_config_coverage` variable.
|
123 |
|
124 | #### The `instrument` command
|
125 |
|
126 | Instruments a single JS file or an entire directory tree and produces an output
|
127 | directory tree with instrumented code. This should not be required for running node
|
128 | unit tests but is useful for tests to be run on the browser.
|
129 |
|
130 | #### The `report` command
|
131 |
|
132 | Writes reports using `coverage*.json` files as the source of coverage information.
|
133 | Reports are available in multiple formats and can be individually configured
|
134 | using the istanbul config file. See `istanbul help report` for more details.
|
135 |
|
136 | #### The `check-coverage` command
|
137 |
|
138 | Checks the coverage of statements, functions, branches, and lines against the
|
139 | provided thresholds. Positive thresholds are taken to be the minimum percentage
|
140 | required and negative numbers are taken to be the number of uncovered entities
|
141 | allowed.
|
142 |
|
143 | ### Ignoring code for coverage
|
144 |
|
145 | * Skip an `if` or `else` path with `/* istanbul ignore if */` or `/* istanbul ignore else */` respectively.
|
146 | * For all other cases, skip the next 'thing' in the source with: `/* istanbul ignore next */`
|
147 |
|
148 | See [ignoring-code-for-coverage.md](ignoring-code-for-coverage.md) for the spec.
|
149 |
|
150 |
|
151 | ### API
|
152 |
|
153 | All the features of istanbul can be accessed as a library.
|
154 |
|
155 | #### Instrument code
|
156 |
|
157 | ```javascript
|
158 | var instrumenter = new require('istanbul').Instrumenter();
|
159 |
|
160 | var generatedCode = instrumenter.instrumentSync('function meaningOfLife() { return 42; }',
|
161 | 'filename.js');
|
162 | ```
|
163 |
|
164 | #### Generate reports given a bunch of coverage JSON objects
|
165 |
|
166 | ```javascript
|
167 | var istanbul = require('istanbul'),
|
168 | collector = new istanbul.Collector(),
|
169 | reporter = new istanbul.Reporter(),
|
170 | sync = false;
|
171 |
|
172 | collector.add(obj1);
|
173 | collector.add(obj2); //etc.
|
174 |
|
175 | reporter.add('text');
|
176 | reporter.addAll([ 'lcov', 'clover' ]);
|
177 | reporter.writeReport(collector, sync, function () {
|
178 | console.log('All reports generated');
|
179 | });
|
180 | ```
|
181 |
|
182 | For the gory details consult the [public API](http://gotwarlost.github.com/istanbul/public/apidocs/index.html)
|
183 |
|
184 |
|
185 | ### License
|
186 |
|
187 | istanbul is licensed under the [BSD License](http://github.com/gotwarlost/istanbul/raw/master/LICENSE).
|
188 |
|
189 | ### Third-party libraries
|
190 |
|
191 | The following third-party libraries are used by this module:
|
192 |
|
193 | * abbrev: https://github.com/isaacs/abbrev-js - to handle command abbreviations
|
194 | * async: https://github.com/caolan/async - for parallel instrumentation of files
|
195 | * escodegen: https://github.com/Constellation/escodegen - for JS code generation
|
196 | * esprima: https://github.com/ariya/esprima - for JS parsing
|
197 | * fileset: https://github.com/mklabs/node-fileset - for loading and matching path expressions
|
198 | * handlebars: https://github.com/wycats/handlebars.js/ - for report template expansion
|
199 | * js-yaml: https://github.com/nodeca/js-yaml - for YAML config file load
|
200 | * mkdirp: https://github.com/substack/node-mkdirp - to create output directories
|
201 | * nodeunit: https://github.com/caolan/nodeunit - dev dependency for unit tests
|
202 | * nopt: https://github.com/isaacs/nopt - for option parsing
|
203 | * once: https://github.com/isaacs/once - to ensure callbacks are called once
|
204 | * resolve: https://github.com/substack/node-resolve - for resolving a post-require hook module name into its main file.
|
205 | * rimraf - https://github.com/isaacs/rimraf - dev dependency for unit tests
|
206 | * which: https://github.com/isaacs/node-which - to resolve a node command to a file for the `cover` command
|
207 | * wordwrap: https://github.com/substack/node-wordwrap - for prettier help
|
208 | * prettify: http://code.google.com/p/google-code-prettify/ - for syntax colored HTML reports. Files checked in under `lib/vendor/`
|
209 |
|
210 | ### Inspired by
|
211 |
|
212 | * YUI test coverage - https://github.com/yui/yuitest - the grand-daddy of JS coverage tools. Istanbul has been specifically designed to offer an alternative to this library with an easy migration path.
|
213 | * cover: https://github.com/itay/node-cover - the inspiration for the `cover` command, modeled after the `run` command in that tool. The coverage methodology used by istanbul is quite different, however
|
214 |
|
215 | ### Shout out to
|
216 |
|
217 | * [mfncooper](https://github.com/mfncooper) - for great brainstorming discussions
|
218 | * [reid](https://github.com/reid), [davglass](https://github.com/davglass), the YUI dudes, for interesting conversations, encouragement, support and gentle pressure to get it done :)
|
219 |
|
220 | ### Why the funky name?
|
221 |
|
222 | Since all the good ones are taken. Comes from the loose association of ideas across
|
223 | coverage, carpet-area coverage, the country that makes good carpets and so on...
|
224 |
|