UNPKG

13.4 kBMarkdownView Raw
1# remap-istanbul
2
3[![Build Status](https://travis-ci.org/SitePen/remap-istanbul.svg?branch=master)](https://travis-ci.org/SitePen/remap-istanbul) [![codecov.io](http://codecov.io/github/SitePen/remap-istanbul/coverage.svg?branch=master)](http://codecov.io/github/SitePen/remap-istanbul?branch=master)
4[![npm version](https://badge.fury.io/js/remap-istanbul.svg)](http://badge.fury.io/js/remap-istanbul)
5[![dependencies Status](https://david-dm.org/SitePen/remap-istanbul/status.svg)](https://david-dm.org/SitePen/remap-istanbul)
6[![devDependencies Status](https://david-dm.org/SitePen/remap-istanbul/dev-status.svg)](https://david-dm.org/SitePen/remap-istanbul?type=dev)
7
8[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/SitePen/remap-istanbul.svg)](http://isitmaintained.com/project/SitePen/remap-istanbul "Average time to resolve an issue")
9[![Percentage of issues still open](http://isitmaintained.com/badge/open/SitePen/remap-istanbul.svg)](http://isitmaintained.com/project/SitePen/remap-istanbul "Percentage of issues still open")
10
11A package that provides the ability to remap [Istanbul](https://gotwarlost.github.io/istanbul/) code coverage information to its original source positions based on a JavaScript [Source Maps v3](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#heading=h.djovrt4kdvga).
12
13`remap-istanbul` requires NodeJS 6 or later.
14
15## How to get Help
16
17This is covered in depth in the [CONTRIBUTING.md](CONTRIBUTING.md#how-to-get-help) document in the repo.
18
19## Usage
20
21For information on how to use `remap-istanbul` with common testing frameworks, please visit the [wiki](https://github.com/SitePen/remap-istanbul/wiki).
22
23There are three main modules that constitute the **remap-istanbul** package:
24
25 - **lib/loadCoverage** - does the basic loading of a Istanbul JSON coverage files. It can "merge" several coverage files, for example if you are collecting remote coverage from other environments and combining it together.
26 - **lib/remap** - does the remapping of the coverage information. It iterates through all the files in the coverage information and looks for JavaScript Source Maps which it will then use to remap the coverage information to the original source.
27 - **lib/writeReport** - a wrapper for the Istanbul report writers to output the any final coverage reports.
28
29### Command Line
30
31The package includes a command line interface which should be placed into the `./node_modules/.bin/remap-istanbul` when the package is installed. The command line interface understands the following argument flags:
32
33|Flag |Description|
34|--------------------|-----------|
35|`-i` or `--input` |The path to the coverage input file. For example `remap-istanbul --input coverage.json`. If omitted, input will be taken from stdin.|
36|`-o` or `--output` |The path to the remapped coverage output file. For example `remap-istanbul --output coverage-final.json`. If omitted, `json` output will be sent to stdout.|
37|`-t` or `--type` |The type of report the output should be. For example `remap-istanbul --output html-report --type html`. If omitted, output defaults to `json`.|
38|`-b` or `--basePath`|When remapping the source files, instead of using the path in the source map, substitute this path.|
39|`-e` or `--exclude`|Pass in a comma seperated string of patterns (exact strings or regexps) to exclude from the output. For example `remap-istanbul --output coverage-final.json --exclude node_modules,tests`|
40
41An example of piping a coverage file to the CLI and writing it out to a file:
42
43```bash
44$ cat coverage-final.json | ./node_modules/.bin/remap-istanbul -o coverage-remapped.json
45```
46
47An example of generating an HTML report off of remapped coverage:
48
49```bash
50$ ./node_modules/.bin/remap-istanbul -i coverage-final.json -o html-report -t html
51```
52
53An example of taking the stdin and writing the stdout in the CLI:
54
55```bash
56$ cat coverage-final.json | ./node_modules/.bin/remap-istanbul > coverage-remap.json
57```
58
59### Basic JavaScript
60
61The main CommonJS module provided combines the modules above into a single API which basic usage can look like this:
62
63```js
64var remapIstanbul = require('remap-istanbul');
65remapIstanbul('coverage-final.json', {
66 'json': 'coverage-final.json'
67});
68```
69
70This would take the coverage file provided. The function accepts the following arguments:
71
72|Argument|Type|Description|
73|--------|----|-----------|
74|sources|Array, string|Either an array of strings or a string the represent the JSON Istanbul files to be remapped|
75|reports|Object|A hash of reports, where the keys are the Istanbul report types and the values are the destination for the report. To send output to the console use the destination null.|
76|*returns*|Promise|A promise that is resolved when all the reports are written|
77
78### AMD
79
80The main modules are provided in AMD for usage (although they utilize `amdefine` to allow transparent loading by a CommonJS loader such as NodeJS's `require` - see blow).
81
82#### `lib/loadCoverage`
83
84The `lib/loadCoverage` module would be used something like this:
85
86```js
87require([ 'remap-istanbul/lib/loadCoverage' ], function (loadCoverage) {
88 var coverage = loadCoverage('coverage-final.json');
89 /* or if you have multiple files you want to merge */
90 coverage = loadCoverage([ 'coverage-ie.json', 'coverage-chrome.json', 'coverage-firefox.json' ]);
91});
92```
93
94The argument signature for `loadCoverage` is:
95
96|Argument|Type|Description|
97|--------|----|-----------|
98|coverage|Array/string|Either an array of strings or a string representing the file path to the coverage file(s).|
99|options|Object?|An object that allows providing alternative methods, mainly used for integration with other systems (e.g. Grunt)|
100|*returns*|Object|A coverage object that is ready to be remapped|
101
102The `options` argument can take the following optional properties:
103
104|Property|Type|Default|Description|
105|--------|----|-------|-----------|
106|readJSON|Function|`JSON.parse(fs.readFileSync)`|A function that will synchronously read a file and return a POJO based on the JSON data in the file|
107|warn|Function|`console.warn`|A function that logs warning messages|
108
109#### `lib/remap`
110
111Usage of the `lib/remap` module would look something like this:
112
113```js
114require([
115 'remap-istanbul/lib/loadCoverage',
116 'remap-istanbul/lib/remap'
117], function (loadCoverage, remap) {
118 var coverage = loadCoverage('coverage-final.json');
119 var collector = remap(coverage); /* collector now contains the remapped coverage */
120});
121```
122
123If the source map no longer points properly at the source files, you can utilize the `basePath` option to override the path retrieved from the source map:
124
125```js
126require([
127 'remap-istanbul/lib/loadCoverage',
128 'remap-istanbul/lib/remap'
129], function (loadCoverage, remap) {
130 var coverage = loadCoverage('coverage-final.json');
131 var collector = remap(coverage, {
132 basePath: 'some/other/path/to/sources'
133 });
134});
135```
136
137The argument signature for `remap` is:
138
139|Argument|Type|Description|
140|--------|----|-----------|
141|coverage|Array/Object|Either an array of coverage objects or a single coverage object.|
142|options|Object?|An object that allows providing alternative methods, mainly used for integration with other systems (e.g. Grunt)|
143|*returns*|istanbul/lib/collector|An Istanbul coverage collector that is ready to be output|
144
145The argument of `options` can contain the following properties:
146
147|Property|Type|Default|Description|
148|--------|----|-------|-----------|
149|basePath|String|Path found in source map|A string to use as the base path for the source locations|
150|exclude|String/RegEx/Function|`undefined`|If the filename of the source coverage file matches the String or RegEx, it will be skipped while mapping the coverage. Optionally, you can use a function that accepts the filename as the argument, and returns true if the file should be skipped.|
151|mapFileName|Function|A function that takes a single string argument that is the remapped file name and returns a string which represents the filename that should be in the mapped coverage.|
152|readFile|Function|`fs.readFileSync`|A function that will synchronously read a file|
153|readJSON|Function|`JSON.parse(fs.readFileSync)`|A function that will synchronously read a file and return a POJO based on the JSON data in the file|
154|warn|Function|`console.warn`|A function that logs warning messages|
155
156#### `lib/writeReport`
157
158The `lib/writeReport` module would be used something like this:
159
160```js
161require([
162 'remap-istanbul/lib/loadCoverage',
163 'remap-istanbul/lib/remap',
164 'remap-istanbul/lib/writeReport'
165], function (loadCoverage, remap, writeReport) {
166 var collector = remap(loadCoverage('coverage-final.json'));
167 writeReport(collector, 'json', 'coverage-final.json').then(function () {
168 /* do something else now */
169 });
170});
171```
172
173The `writeReport` function can take the following arguments:
174
175|Argument|Type|Description|
176|--------|----|-----------|
177|collector|istanbul/lib/collector|This is an Istanbul coverage collector (usually returned from `remap` which is to be written out in a report)|
178|reportType|string|The type of the report. Valid values are: `clover`, `cobertura`, `html`, `json-summary`, `json`, `file`, `lcovonly`, `teamcity`, `text-lcov`, `text-summary` or `text`|
179|reportOptions|object|Options object of key/value pairs to pass to the reporter|
180|dest|string, Function|The destination file, directory or console logging function that is the destination of the report. Only `text-lcov` takes the logging function and will default to `console.log` if no value is passed.|
181|*returns*|Promise|A promise that is resolved when the report is written.|
182
183### CommonJS
184
185If you are not using an AMD loader, that is not a problem for consuming the modules. They also can be loaded in a
186CommonJS environment:
187
188```js
189var loadCoverage = require('remap-istanbul/lib/loadCoverage');
190var remap = require('remap-istanbul/lib/remap');
191var writeReport = require('remap-istanbul/lib/writeReport');
192```
193
194### Grunt Task
195
196You can utilize this package as a [Grunt](http://gruntjs.com) task. After installing it as a package, you need to add the following to your `Gruntfile.js`:
197
198```js
199grunt.loadNpmTasks('remap-istanbul');
200```
201
202The task is a multi-target task and a basic configuration for the task would look something like this:
203
204```js
205grunt.initConfig({
206 remapIstanbul: {
207 build: {
208 src: 'coverage-final.json',
209 options: {
210 reports: {
211 'lcovhtml': 'html-report',
212 'json': 'coverage-final.json'
213 }
214 }
215 }
216 }
217});
218```
219
220This would take in `coverage-final.json`, remap it and then output the Istanbul HTML report to `html-report`
221and overwrite the original `coverage-final.json`.
222
223The task also recognizes an abbreviated version of configuration:
224
225```js
226grunt.initConfig({
227 remapIstanbul: {
228 build: {
229 files: [ {
230 src: 'coverage.json',
231 dest: 'tmp/coverage.json',
232 type: 'json'
233 } ]
234 }
235 }
236});
237```
238
239By default, the grunt task will log warnings/errors to the `grunt.log.error`. If instead you wish the
240grunt task to `grunt.fail.warn` which will require `--force` to ensure the task does not fail the whole
241build, you should supply the `fail` option in the task configuration:
242
243```js
244grunt.initConfig({
245 remapIstanbul: {
246 build: {
247 src: 'coverage-final.json',
248 options: {
249 fail: true
250 }
251 }
252 }
253});
254```
255
256### Gulp Plugin
257
258You can utilize this package as a [gulp](http://gulpjs.com) plugin. There are two main ways it can be
259used. Just taking a coverage file, remapping and outputting it would look like this:
260
261```js
262var gulp = require('gulp');
263var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
264
265gulp.task('remap-istanbul', function () {
266 return gulp.src('coverage-final.json')
267 .pipe(remapIstanbul())
268 .pipe(gulp.dest('coverage-remapped.json'));
269});
270```
271
272Another way is to utilize the plugin is to have the plugin write out the Istanbul reports directly.
273This can be accomplished by passing a `reports` property in the options. For example, to have the JSON
274coverage report output in addition to the HTML coverage report, at task would look like this:
275
276```js
277var gulp = require('gulp');
278var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
279
280gulp.task('remap-istanbul', function () {
281 return gulp.src('coverage-final.json')
282 .pipe(remapIstanbul({
283 reports: {
284 'json': 'coverage.json',
285 'html': 'html-report'
286 }
287 }));
288});
289```
290
291By default, errors in the gulp task will be considered non-fatal and will just be logged to the
292console. If you wish errors to be emitted and fail the task, you need to supply the task with
293`fail` being truthy:
294
295```js
296var gulp = require('gulp');
297var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
298
299gulp.task('remap-istanbul', function () {
300 return gulp.src('coverage-final.json')
301 .pipe(remapIstanbul({
302 fail: true
303 }));
304});
305```
306
307### Intern Reporter
308
309The package comes with an [Intern](https://theintern.github.io/) reporter that makes it easy to output the `coverage.json` from a test run. The most common usage from the command line would be something like:
310
311```sh
312node_modules/.bin/intern-runner config=tests/intern reporters=Console reporters=node_modules/remap-istanbul/lib/intern-reporters/JsonCoverage
313```
314
315This will output a `coverage-final.json` in the root of your project, which you can use with the rest of `remap-istanbul` to remap it back to the source files.