OSDN Git Service

Regular updates
[twpd/master.git] / jscoverage.md
1 ---
2 title: jscoverage
3 category: JavaScript libraries
4 layout: 2017/sheet
5 intro: |
6   A small guide into installing [jscoverage](https://npmjs.com/package./jscoverage). Also see [mocha-blanket](./mocha-blanket).
7 ---
8
9 ### Install
10
11 #### Install via npm
12
13 ```bash
14 npm install --save-dev jscoverage
15 ```
16
17 #### Ignore output
18
19 ```bash
20 echo coverage.html >> .gitignore
21 ```
22
23 ### package.json
24
25 The `coverage` task injects your source files (`lib`) with jscoverage hooks, runs `mocha -R html-cov`, then restores later.
26 {: .-setup}
27
28 ```bash
29 /* directory */
30 "coverage": "mv lib lib~; (jscoverage lib~ lib; mocha -R html-cov > coverage.html); rm -rf lib; mv lib~ lib"
31 ```
32 {: .-hard-wrap}
33
34 ```bash
35 /* single file */
36 "coverage": "(cp index.js index.js~; jscoverage index.js; mv index-cov.js index.js; mocha -R html-cov > coverage.html); mv index.js~ index.js"
37 ```
38 {: .-hard-wrap}
39
40 ### Run
41
42 ```bash
43 npm run coverage
44 ```
45
46 ```bash
47 open coverage.html
48 ```
49
50 ### Caveats
51
52 If you're using jsdom, be sure to expose the `window._$jscoverage` variable into 
53 the `global` scope.