UNPKG

8.59 kBMarkdownView Raw
1# Node Codacy Coverage
2
3Credits to [David](https://github.com/DavidTPate) for creating this!
4[Codacy](https://codacy.com/) support for Node.js. Get coverage reporting and code analysis for Node.js from Codacy.
5
6[![Codacy](https://api.codacy.com/project/badge/grade/3c7f5de6ce734762981d3e689de7b941)](https://www.codacy.com/app/codacy/node-codacy-coverage)
7[![Codacy](https://api.codacy.com/project/badge/coverage/3c7f5de6ce734762981d3e689de7b941)](https://www.codacy.com/app/codacy/node-codacy-coverage)
8[![Build Status](https://circleci.com/gh/codacy/node-codacy-coverage.png?style=shield&circle-token=:circle-token)](https://circleci.com/gh/codacy/node-codacy-coverage)
9[![npm](https://img.shields.io/npm/v/codacy-coverage.svg)](https://www.npmjs.com/package/codacy-coverage)
10[![npm](https://img.shields.io/npm/dm/codacy-coverage.svg)](https://www.npmjs.com/package/codacy-coverage)
11[![David](https://img.shields.io/david/codacy/node-codacy-coverage.svg)](https://david-dm.org/codacy/node-codacy-coverage)
12[![David](https://img.shields.io/david/dev/codacy/node-codacy-coverage.svg)](https://david-dm.org/codacy/node-codacy-coverage)
13[![David](https://img.shields.io/david/peer/codacy/node-codacy-coverage.svg)](https://david-dm.org/codacy/node-codacy-coverage)
14
15## Installation
16
17Add the latest version of `codacy-coverage` to your package.json:
18
19```sh
20npm install codacy-coverage --save
21```
22
23If you're using mocha, add `mocha-lcov-reporter` to your package.json:
24
25```sh
26npm install mocha-lcov-reporter --save
27```
28
29## Enterprise
30
31To send coverage in the enterprise version you should specify your Codacy installation URL with the option `-e`:
32
33```sh
34codacy-coverage -e <YOUR-CODACY-ENTERPRISE-URL>:16006
35```
36
37## Usage
38
39This cli can take standard input from any tool that emits the lcov data format (including [mocha](http://mochajs.org)'s [LCov reporter](https://npmjs.org/package/mocha-lcov-reporter)) and send it to Codacy to report your code coverage there.
40
41Once your app is instrumented for coverage, and building, you need to pipe the lcov output to `codacy-coverage`.
42
43### Identifying the project
44
45You'll need to provide the secret Project API token from `Codacy Project > Settings > Integrations > Project API` via:
46
47* (Recommended) Environment variable: CODACY_PROJECT_TOKEN
48* CLI parameter variable: `--token`
49
50> Note: You should keep your any API token well **protected**, as it grants owner permissions to your projects.
51
52### Test Coverage
53
54#### [Mocha](http://mochajs.org) + [Blanket.js](https://github.com/alex-seville/blanket)
55
56* Install [blanket.js](http://blanketjs.org/)
57* Configure blanket according to [docs](https://github.com/alex-seville/blanket/blob/master/docs/getting_started_node.md).
58* Add test with coverage step to your package.json:
59
60```json
61"scripts": {
62 "test-with-coverage": "NODE_ENV=test YOURPACKAGE_COVERAGE=1 mocha --require blanket --reporter mocha-lcov-reporter | codacy-coverage"
63}
64```
65
66* Run your tests with:
67
68```sh
69npm run test-with-coverage
70```
71
72#### [Mocha](http://mochajs.org) + [JSCoverage](https://github.com/fishbar/jscoverage)
73
74Instrumenting your app for coverage is probably harder than it needs to be (read [here](http://www.seejohncode.com/2012/03/13/setting-up-mocha-jscoverage/)), but that's also a necessary step.
75
76* Add test with coverage step to your package.json:
77
78```json
79"scripts": {
80 "test-with-coverage": "YOURPACKAGE_COVERAGE=1 mocha test -R mocha-lcov-reporter | codacy-coverage"
81}
82```
83
84* Run your tests with:
85
86```sh
87npm run test-with-coverage
88```
89
90#### [Istanbul](https://github.com/gotwarlost/istanbul)
91
92##### With Mocha
93
94* Add test with coverage step to your package.json:
95
96```json
97"scripts": {
98 "test-with-coverage": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | codacy-coverage && rm -rf ./coverage"
99}
100```
101
102* Run your tests with:
103
104```sh
105npm run test-with-coverage
106```
107
108##### With Jasmine
109
110* Add test with coverage step to your package.json:
111
112```json
113"scripts": {
114 "test-with-coverage": "istanbul cover jasmine-node --captureExceptions spec/ && cat ./coverage/lcov.info | codacy-coverage && rm -rf ./coverage"
115}
116```
117
118* Run your tests with:
119
120```sh
121npm run test-with-coverage
122```
123
124#### [Poncho](https://github.com/deepsweet/poncho)
125
126Client-side JS code coverage using [PhantomJS](https://github.com/ariya/phantomjs), [Mocha](http://mochajs.org) and [Blanket](https://github.com/alex-seville/blanket):
127
128* [Configure](http://mochajs.org#browser-support) Mocha for browser
129* [Mark](https://github.com/deepsweet/poncho#usage) target script(s) with `data-cover` html-attribute
130* Add test with coverage step to your package.json:
131
132```json
133"scripts": {
134 "test-with-coverage": "poncho -R lcov test/test.html | codacy-coverage"
135}
136```
137
138* Run your tests with:
139
140```sh
141npm run test-with-coverage
142```
143
144#### [Jest](https://facebook.github.io/jest/)
145
146* Add test with coverage step to your package.json:
147
148Note: [jest might return exit code 1](https://github.com/facebook/jest/issues/3520) if you defined a coverage threshold and the threshold is not met
149
150```json
151"scripts": {
152 "test-with-coverage": "jest --coverage && cat ./coverage/lcov.info | codacy-coverage"
153}
154```
155
156* Run your tests with:
157
158```sh
159npm run test-with-coverage
160```
161
162## Extras
163
164### Account Token
165
166As an alternative to the Project API token you can also send coverage using your account/api token by following steps:
167
168* Add test with coverage step to your package.json:
169
170```json
171"scripts": {
172 "test-with-coverage": "cat ./coverage/lcov.info | codacy-coverage --accountToken <account-token> --username <username> --projectName <project-name>"
173}
174```
175
176* Run your tests with:
177
178```sh
179npm run test-with-coverage
180```
181
182You'll need to provide the secret Account API token from [Codacy Account](https://app.codacy.com/account/apiTokens)` > API Tokens` via:
183
184* (Recommended) Environment variable: CODACY_ACCOUNT_TOKEN
185* CLI parameter variable: `--accountToken`
186
187### Force custom language (e.g. Typescript, Coffeescript, C, ...)
188
189* Pass an extra parameter to the codacy-coverage reporter `--language typescript` or `--language coffeescript`.
190* If you have multiple languages you need to invoke the reporter for each of them.
191
192### Run in Windows
193
194If you are running coverage in a windows machine without Unix tools,
195you need to change the command to `codacy-coverage < ./test/unit/coverage/lcov.info`.
196
197## Troubleshooting
198
199### Path Problems
200
201The paths in your coverage file should be relative,
202if you are having problems with absolute paths,
203you can run our plugin with `-p .` to strip the current path from the paths in your coverage file:
204
205```json
206"scripts": {
207 "test-with-coverage": "cat ./coverage/lcov.info | codacy-coverage -p ."
208}
209```
210
211### Enterprise Coverage
212
213To send coverage in the **enterprise** version you should specify your Codacy installation URL followed by the port 16006 using the -e option, example:
214
215```json
216"scripts": {
217 "test-with-coverage": "cat ./coverage/lcov.info | codacy-coverage -e <YOUR-CODACY-ENTERPRISE-URL>:16006"
218}
219```
220
221## Options
222
223```bash
224Options:
225
226 -V, --version output the version number
227 -f, --format [value] Coverage input format
228 -t, --token [value] Codacy Project API Token
229 -a, --accountToken [value] Codacy Account Token
230 -u, --username [value] Codacy Username/Organization
231 -n, --projectName [value] Codacy Project Name
232 -c, --commit [value] Commit SHA hash
233 -l, --language [value Project Language
234 -e, --endpoint [value] Codacy API Endpoint
235 -p, --prefix [value] Project path prefix
236 -v, --verbose Display verbose output
237 -d, --debug Display debug output
238 -h, --help output usage information
239```
240
241## License
242
243[MIT](LICENSE)
244
245## What is Codacy
246
247[Codacy](https://www.codacy.com/) is an Automated Code Review Tool that monitors your technical debt,
248helps you improve your code quality,
249teaches best practices to your developers,
250and helps you save time in Code Reviews.
251
252### Among Codacy’s features
253
254* Identify new Static Analysis issues
255* Commit and Pull Request Analysis with GitHub, BitBucket/Stash, GitLab (and also direct git repositories)
256* Auto-comments on Commits and Pull Requests
257* Integrations with Slack, HipChat, Jira, YouTrack
258* Track issues Code Style, Security, Error Proneness, Performance, Unused Code and other categories
259
260Codacy also helps keep track of Code Coverage, Code Duplication, and Code Complexity.
261
262Codacy supports PHP, Python, Ruby, Java, JavaScript, and Scala, among others.
263
264### Free for Open Source
265
266Codacy is free for Open Source projects.