UNPKG

3.76 kBMarkdownView Raw
1mocha-lcov-reporter
2===================
3
4LCOV reporter for Mocha.
5
6LCOV format can be found in this [geninfo manpage](http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php). This LCOV reporter was built after [Sonar Javascript Plugin LCOVParser class](https://github.com/SonarCommunity/sonar-javascript/blob/master/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/lcov/LCOVParser.java).
7
8Usage
9=====
10
11The mocha-lcov-reporter is a reporter for mocha. In order to get coverage data, the same instructions are to be followed as for the `JSONCov` and `HTMLCov` reporters:
12
13- Install [jscover](https://github.com/node-modules/jscover) or [node-jscoverage](https://github.com/visionmedia/node-jscoverage)
14- Instrument your library with `jscover` (or `node-jscoverage`)
15- Run your tests against your instrumented library and save the output
16
17For example, the following script can be part of your build process (add `jscover`, `mocha`, and `mocha-lcov-reporter` as `devDependencies` to your `package.json` file and run `npm install`):
18
19```
20#!/usr/bin/env bash
21rm -rf coverage
22rm -rf lib-cov
23
24mkdir coverage
25
26node_modules/.bin/jscover lib lib-cov
27mv lib lib-orig
28mv lib-cov lib
29node_modules/.bin/mocha -R mocha-lcov-reporter > coverage/coverage.lcov
30rm -rf lib
31mv lib-orig lib
32```
33
34This script instruments your sources (source 'lib', target 'lib-cov'), temporarily replaces your library by the instrumented version, run the tests against the instrumented version of your sources, and undoes the replacing of the original library by the instrumented library.
35
36A safer and better approach is to instrument your library (target 'lib-cov'), and include that directory from your tests directly. Instead of doing a 'require("../lib")' do a 'require("../lib-cov")'. This saves the hassle of replacing directory 'lib' with directory 'lib-cov' and undoing it afterwards. You can use an environment variable to check if the instrumented library should be included or the normal version:
37
38```
39var lib = process.env.JSCOV ? require('../lib-cov') : require('../lib');
40```
41
42And to get the test-coverage, run mocha as follows:
43
44```
45JSCOV=1 mocha -R mocha-lcov-reporter > coverage/coverage.lcov
46```
47
48See the [SaXPath project](https://github.com/StevenLooman/saxpath) for an example on how to do this.
49
50Incomplete paths in LCOV output
51===============================
52
53Unfortunately, when the code is instrumented using `jscover` or `node-jscoverage`, the output of the reporter will have incomplete paths for the covered files. A quick fix is to update the paths after running the tests with the mocha-lcov-reporter, like so:
54
55```
56# run the tests
57JS_COV=1 ./node_modules/.bin/mocha -R mocha-lcov-reporter > coverage/coverage_temp.lcov
58
59# fix the paths
60sed 's,SF:,SF:lib/,' coverage/coverage_temp.lcov > coverage/coverage.lcov
61```
62
63The reason this is that `jscover` runs on the directory you specify (e.g., `lib/`) and regards that as the root for the project.
64
65Blanket support
66===============
67
68[Blanket.js](http://blanketjs.org/) can be used as well. After the lcov file, be sure to fix the paths for the covered files. The path will be an URL, having `file:` as its protocol. Using the same manner as above, the path can be fixed using `sed`.
69
70Example output
71==============
72
73What does LCOV output look like? LCOV is meant to be interpreted by other programs and not meant to be readable by humans. This is an example:
74
75```
76SF:base_unit.js
77DA:1,1
78DA:4,1
79DA:5,155
80DA:7,155
81DA:8,140
82DA:9,140
83DA:12,155
84DA:13,155
85DA:16,1
86DA:17,1
87DA:20,1
88DA:21,9
89DA:24,1
90DA:25,40
91DA:28,1
92DA:29,26
93DA:32,1
94DA:33,7
95DA:36,1
96DA:37,6
97DA:40,1
98DA:41,45
99DA:44,1
100DA:45,52
101DA:51,1
102DA:52,3
103DA:55,1
104end_of_record
105```
106
107If you are looking for something human readable, the `HTMLCov` reporter can be used.