1 | mocha-lcov-reporter
|
2 | ===================
|
3 |
|
4 | LCOV reporter for Mocha.
|
5 |
|
6 | LCOV 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 |
|
8 | Usage
|
9 | =====
|
10 |
|
11 | The 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 |
|
17 | For 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
|
21 | rm -rf coverage
|
22 | rm -rf lib-cov
|
23 |
|
24 | mkdir coverage
|
25 |
|
26 | node_modules/.bin/jscover lib lib-cov
|
27 | mv lib lib-orig
|
28 | mv lib-cov lib
|
29 | node_modules/.bin/mocha -R mocha-lcov-reporter > coverage/coverage.lcov
|
30 | rm -rf lib
|
31 | mv lib-orig lib
|
32 | ```
|
33 |
|
34 | This 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 |
|
36 | A 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 | ```
|
39 | var lib = process.env.JSCOV ? require('../lib-cov') : require('../lib');
|
40 | ```
|
41 |
|
42 | And to get the test-coverage, run mocha as follows:
|
43 |
|
44 | ```
|
45 | JSCOV=1 mocha -R mocha-lcov-reporter > coverage/coverage.lcov
|
46 | ```
|
47 |
|
48 | See the [SaXPath project](https://github.com/StevenLooman/saxpath) for an example on how to do this.
|
49 |
|
50 | Incomplete paths in LCOV output
|
51 | ===============================
|
52 |
|
53 | Unfortunately, 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
|
57 | JS_COV=1 ./node_modules/.bin/mocha -R mocha-lcov-reporter > coverage/coverage_temp.lcov
|
58 |
|
59 | # fix the paths
|
60 | sed 's,SF:,SF:lib/,' coverage/coverage_temp.lcov > coverage/coverage.lcov
|
61 | ```
|
62 |
|
63 | The reason this is that `jscover` runs on the directory you specify (e.g., `lib/`) and regards that as the root for the project.
|
64 |
|
65 | Blanket 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 |
|
70 | Example output
|
71 | ==============
|
72 |
|
73 | What 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 | ```
|
76 | SF:base_unit.js
|
77 | DA:1,1
|
78 | DA:4,1
|
79 | DA:5,155
|
80 | DA:7,155
|
81 | DA:8,140
|
82 | DA:9,140
|
83 | DA:12,155
|
84 | DA:13,155
|
85 | DA:16,1
|
86 | DA:17,1
|
87 | DA:20,1
|
88 | DA:21,9
|
89 | DA:24,1
|
90 | DA:25,40
|
91 | DA:28,1
|
92 | DA:29,26
|
93 | DA:32,1
|
94 | DA:33,7
|
95 | DA:36,1
|
96 | DA:37,6
|
97 | DA:40,1
|
98 | DA:41,45
|
99 | DA:44,1
|
100 | DA:45,52
|
101 | DA:51,1
|
102 | DA:52,3
|
103 | DA:55,1
|
104 | end_of_record
|
105 | ```
|
106 |
|
107 | If you are looking for something human readable, the `HTMLCov` reporter can be used.
|