|
1 | | -# [Codecov][0] NodeJS / Javascript Example |
| 1 | +# [Codecov](https://codecov.io) NodeJS / JavaScript Example |
| 2 | +[](https://app.codecov.io/github/codecov/example-node) |
2 | 3 | [](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-node?ref=badge_shield) |
3 | 4 |
|
| 5 | +This example repository shows how Codecov can be integrated with a simple Node.js project. It uses **GitHub Actions** as the CI/CD provider and the built-in [Node.js test runner](https://nodejs.org/api/test.html) with [`c8`](https://github.com/bcoe/c8) as the coverage provider. |
4 | 6 |
|
5 | | -## Guide |
| 7 | +## Running locally |
6 | 8 |
|
7 | | -You can install Codecov by adding |
8 | 9 | ```sh |
9 | | -npm install -g codecov |
| 10 | +npm install |
| 11 | +npm test |
10 | 12 | ``` |
11 | 13 |
|
12 | | -### Travis Setup |
13 | | - |
14 | | -Add the following to your `.travis.yml`: |
15 | | - |
16 | | -```yml |
17 | | -language: |
18 | | - node_js |
19 | | -before_install: |
20 | | - - npm install -g codecov |
21 | | -script: |
22 | | - - istanbul cover ./node_modules/mocha/bin/_mocha --reporter lcovonly -- -R spec |
23 | | - - codecov |
24 | | - |
25 | | -``` |
26 | | - |
27 | | -The first script line will change depending on your coverage collecting tool, see below. |
28 | | -### Produce Coverage Reports |
29 | | -### [Mocha](http://mochajs.org/) + [Blanket.js](https://github.com/alex-seville/blanket) |
30 | | -- Install [blanket.js](http://blanketjs.org/) |
31 | | -- Configure blanket according to [docs](https://github.com/alex-seville/blanket/blob/master/docs/getting_started_node.md). |
32 | | -- Run your tests with a command like this: |
33 | | - |
34 | | -```sh |
35 | | -NODE_ENV=test YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha \ |
36 | | - --require blanket \ |
37 | | - --reporter mocha-lcov-reporter |
38 | | -codecov |
39 | | -``` |
40 | | -### [Mocha](http://mochajs.org/) + [JSCoverage](https://github.com/fishbar/jscoverage) |
41 | | - |
42 | | -Instrumenting 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. |
43 | | - |
44 | | -In mocha, if you've got your code instrumented for coverage, the command for a travis build would look something like this: |
45 | | -```sh |
46 | | -YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha test -R mocha-lcov-reporter |
47 | | -``` |
48 | | - |
49 | | -### [Istanbul](https://istanbul.js.org/) |
50 | | - |
51 | | -**With Mocha:** |
52 | | - |
53 | | -```sh |
54 | | -nyc --reporter=lcov mocha && codecov |
55 | | -``` |
56 | | - |
57 | | -**With Jasmine:** |
58 | | - |
59 | | -```sh |
60 | | -istanbul cover ./node_modules/jasmine/bin/jasmine.js |
61 | | -``` |
62 | | - |
63 | | -***With Karma:*** |
64 | | - |
65 | | -The `lcov.info` can be used as in other configurations. Some projects experienced better results using `json` output but it is no longer enabled by default. In `karma.config.js` both can be enabled: |
66 | | - |
67 | | -```javascript |
68 | | -module.exports = function karmaConfig (config) { |
69 | | - config.set({ |
70 | | - ... |
71 | | - reporters: [ |
72 | | - ... |
73 | | - // Reference: https://github.com/karma-runner/karma-coverage |
74 | | - // Output code coverage files |
75 | | - 'coverage' |
76 | | - ], |
77 | | - // Configure code coverage reporter |
78 | | - coverageReporter: { |
79 | | - reporters: [ |
80 | | - // generates ./coverage/lcov.info |
81 | | - {type:'lcovonly', subdir: '.'}, |
82 | | - // generates ./coverage/coverage-final.json |
83 | | - {type:'json', subdir: '.'}, |
84 | | - ] |
85 | | - }, |
86 | | - ... |
87 | | - }); |
88 | | -}; |
89 | | -``` |
90 | | - |
91 | | -In `package.json` supply either `lcov.info` or `coverage-final.json` to `codecov`: |
92 | | - |
93 | | -```javascript |
94 | | -{ |
95 | | - "scripts": { |
96 | | - "report-coverage": "codecov", |
97 | | - ... |
98 | | - } |
99 | | - ... |
100 | | -} |
101 | | -``` |
102 | | - |
103 | | -### [Nodeunit](https://github.com/caolan/nodeunit) + [JSCoverage](https://github.com/fishbar/jscoverage) |
104 | | - |
105 | | -Depend on nodeunit and jscoverage: |
106 | | - |
107 | | -```sh |
108 | | -npm install nodeunit jscoverage codecov --save-dev |
109 | | -``` |
110 | | - |
111 | | -Add a codecov script to "scripts" in your `package.json`: |
112 | | - |
113 | | -```javascript |
114 | | -"scripts": { |
115 | | - "test": "nodeunit test", |
116 | | - "codecov": "jscoverage lib && YOURPACKAGE_COVERAGE=1 nodeunit --reporter=lcov test && codecov" |
117 | | -} |
118 | | -``` |
119 | | - |
120 | | -Ensure your app requires instrumented code when `process.env.YOURPACKAGE_COVERAGE` variable is defined. |
121 | | - |
122 | | -Run your tests with a command like this: |
123 | | - |
124 | | -```sh |
125 | | -npm run codecov |
126 | | -``` |
127 | | - |
128 | | -### [GitHub Actions](https://github.com/codecov/codecov-action) |
129 | | - |
130 | | -```yaml |
131 | | -- name: Codecov |
132 | | - uses: codecov/codecov-action@v2 |
133 | | - with: |
134 | | - token: ${{ secrets.CODECOV_TOKEN }} |
135 | | - flags: unittests |
136 | | -``` |
137 | | -
|
138 | | -### [Poncho](https://github.com/deepsweet/poncho) |
139 | | -Client-side JS code coverage using [PhantomJS](https://github.com/ariya/phantomjs), [Mocha](http://mochajs.org/) and [Blanket](https://github.com/alex-seville/blanket): |
140 | | -- [Configure](http://mochajs.org/#running-mocha-in-the-browser) Mocha for browser |
141 | | -- [Mark](https://github.com/deepsweet/poncho#usage) target script(s) with `data-cover` html-attribute |
142 | | -- Run your tests with a command like this: |
143 | | - |
144 | | -```sh |
145 | | -./node_modules/.bin/poncho -R lcov test/test.html && codecov |
146 | | -``` |
147 | | - |
148 | | -### [Lab](https://github.com/hapijs/lab) |
149 | | -```sh |
150 | | -istanbul cover ./node_modules/lab/bin/lab --report lcovonly -- -l && codecov |
151 | | -``` |
152 | | - |
153 | | -### [nyc](https://github.com/bcoe/nyc) |
154 | | -```javascript |
155 | | -{ |
156 | | - "scripts": { |
157 | | - "report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", |
158 | | - ... |
159 | | - } |
160 | | - ... |
161 | | -} |
162 | | -``` |
163 | | - |
164 | | -### [Jest](https://facebook.github.io/jest/) |
165 | | -Run |
166 | | -```sh |
167 | | -jest --ci --coverage && codecov |
168 | | -``` |
169 | | - |
170 | | -or you can add it in your package.json: |
171 | | -```javascript |
172 | | -"jest": { |
173 | | - "coverageDirectory": "./coverage/", |
174 | | - "collectCoverage": true |
175 | | -} |
176 | | -``` |
177 | | -Jest will now generate coverage files into `coverage/` and you can run your tests with a command like this: |
178 | | -```sh |
179 | | -jest && codecov |
180 | | -``` |
181 | | - |
182 | | -### JSX |
183 | | -There have been reports of [gotwarlost/istanbul](https://github.com/gotwarlost/istanbul) not working properly with JSX files, which provide inaccurate coverage results. Please try using [ambitioninc/babel-istanbul](https://github.com/ambitioninc/babel-istanbul). |
184 | | - |
185 | | -### [Tape](https://github.com/substack/tape) |
186 | | - |
187 | | -``` |
188 | | -istanbul cover node_modules/.bin/tape ./test/*.js |
189 | | -``` |
190 | | -
|
191 | | -Or simply run: |
192 | | -
|
193 | | -``` |
194 | | -istanbul cover ./test/*.js |
195 | | -``` |
196 | | -
|
197 | | -Or add in package.json: |
198 | | -
|
199 | | -``` |
200 | | -"test": "istanbul cover test/*.js", |
201 | | -``` |
202 | | -
|
203 | | -After test: |
204 | | -
|
205 | | -``` |
206 | | -codecov --token=:token |
207 | | -``` |
208 | | -
|
209 | | -## Caveats |
210 | | -#### Private Repo |
211 | | -Repository tokens are required for (a) all private repos, (b) public repos not using Travis-CI, CircleCI or AppVeyor. Find your repository token at Codecov and provide via `codecov --token=:token` or `export CODECOV_TOKEN=":token"` |
212 | | -
|
213 | | -## Common Pitfalls |
214 | | -
|
215 | | -### [`mock-fs`](https://github.com/tschaub/mock-fs) |
216 | | -When using `mock-fs` make sure to run `mock.restore()` when your tests are done running, or else the reports wont get generated on the CI. |
217 | | -
|
218 | | -## Support |
219 | | -
|
220 | | -### FAQ |
221 | | -- Q: Is there a TypeScript example?<br/>A: Yes [codecov/example-typescript](https://github.com/codecov/example-typescript). |
222 | | -
|
223 | | -1. More documentation at https://docs.codecov.io |
224 | | -2. Configure codecov through the `codecov.yml` https://docs.codecov.io/docs/codecov-yaml |
225 | | -
|
226 | | -[0]: https://codecov.io/ |
227 | | -
|
228 | | -We are happy to help if you have any questions. Please contact email our Support at [support@codecov.io](mailto:support@codecov.io) |
| 14 | +This runs the test suite and writes an `lcov` coverage report to `coverage/` along with a JUnit report at `junit.xml`. |
229 | 15 |
|
| 16 | +## Links |
| 17 | +- [Quick Start](https://docs.codecov.com/docs/quick-start) |
| 18 | +- [GitHub Tutorial](https://docs.codecov.com/docs/github-tutorial) |
| 19 | +- [Community Boards](https://community.codecov.io) |
| 20 | +- [Support](https://codecov.io/support) |
| 21 | +- [Documentation](https://docs.codecov.io) |
230 | 22 |
|
231 | 23 | ## License |
232 | 24 | [](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-node?ref=badge_large) |
0 commit comments