cheatsheets/mocha-blanket.md

68 lines
1.2 KiB
Markdown
Raw Permalink Normal View History

2014-11-05 06:14:00 +00:00
---
title: Mocha blanket
2015-11-24 05:02:17 +00:00
category: JavaScript libraries
2017-10-27 04:52:26 +00:00
intro: |
Use [blanket](https://npmjs.com/package/blanket) for easy coverage reporting for Mocha JavaScript tests.
2014-11-05 06:14:00 +00:00
---
2017-10-27 04:52:26 +00:00
### Quickstart guide
2014-11-05 06:14:00 +00:00
2017-10-27 04:52:26 +00:00
Install blanket:
{: .-setup}
2014-11-05 06:14:00 +00:00
2017-10-27 04:52:26 +00:00
```bash
npm i --save-dev blanket
```
2014-11-05 06:14:00 +00:00
In your test helpers, use Blanket before `require`ing:
```js
if (process.env.COVERAGE) {
require('blanket')({
pattern: require('path').resolve('./index.js')
});
}
thing = require('../index');
```
Add to `package.json`:
```json
"scripts": {
2014-11-06 08:36:56 +00:00
"coverage": "env COVERAGE=1 mocha -R html-cov > coverage.html && open coverage.html"
2014-11-05 06:14:00 +00:00
}
```
Be sure to ignore it:
2017-10-27 04:52:26 +00:00
```bash
echo "coverage.html" >> .gitignore
```
2014-11-05 06:14:00 +00:00
Then run:
2017-10-27 04:52:26 +00:00
```bash
npm run coverage
```
2014-11-05 06:14:00 +00:00
2017-10-27 04:52:26 +00:00
### Travis + coveralls.io support
2014-11-05 06:14:00 +00:00
Visit [coveralls.io] then activate your repo. Then install the appropriate packages:
2017-10-27 04:52:26 +00:00
{: .-setup}
2014-11-05 06:14:00 +00:00
2017-10-27 04:52:26 +00:00
```bash
npm i --save-dev mocha-lcov-reporter coveralls
```
2014-11-05 06:14:00 +00:00
Add this to `.travis.yml`:
```yml
after_success:
- ./node_modules/.bin/mocha -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
```
Commit, push, wait for Travis to finish.
[blanket]: https://www.npmjs.org/package/blanket
2014-11-06 08:36:56 +00:00
[coveralls.io]: http://coveralls.io