OSDN Git Service

Regular updates
[twpd/master.git] / mocha-blanket.md
1 ---
2 title: Mocha blanket
3 category: JavaScript libraries
4 layout: 2017/sheet
5 intro: |
6   Use [blanket](https://npmjs.com/package/blanket) for easy coverage reporting for Mocha JavaScript tests.
7 ---
8
9 ### Quickstart guide
10
11 Install blanket:
12 {: .-setup}
13
14 ```bash
15 npm i --save-dev blanket
16 ```
17
18 In your test helpers, use Blanket before `require`ing:
19
20 ```js
21 if (process.env.COVERAGE) {
22   require('blanket')({
23     pattern: require('path').resolve('./index.js')
24   });
25 }
26 thing = require('../index');
27 ```
28
29 Add to `package.json`:
30
31 ```json
32 "scripts": {
33   "coverage": "env COVERAGE=1 mocha -R html-cov > coverage.html && open coverage.html"
34 }
35 ```
36
37 Be sure to ignore it:
38
39 ```bash
40 echo "coverage.html" >> .gitignore
41 ```
42
43 Then run:
44
45 ```bash
46 npm run coverage
47 ```
48
49 ### Travis + coveralls.io support
50
51 Visit [coveralls.io] then activate your repo. Then install the appropriate packages:
52 {: .-setup}
53
54 ```bash
55 npm i --save-dev mocha-lcov-reporter coveralls
56 ```
57
58 Add this to `.travis.yml`:
59
60 ```yml
61 after_success:
62   - ./node_modules/.bin/mocha -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
63 ```
64
65 Commit, push, wait for Travis to finish.
66
67 [blanket]: https://www.npmjs.org/package/blanket
68 [coveralls.io]: http://coveralls.io