Awesome Open Source
Awesome Open Source

Codecov NodeJS / Javascript Example

FOSSA Status


You can install Codecov by adding

npm install -g codecov

Travis Setup

Add the following to your .travis.yml:

  - npm install -g codecov
  - istanbul cover ./node_modules/mocha/bin/_mocha --reporter lcovonly -- -R spec
  - codecov

The first script line will change depending on your coverage collecting tool, see below.

Produce Coverage Reports

Mocha + Blanket.js

  • Install blanket.js
  • Configure blanket according to docs.
  • Run your tests with a command like this:
NODE_ENV=test YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha \
  --require blanket \
  --reporter mocha-lcov-reporter

Mocha + JSCoverage

Instrumenting your app for coverage is probably harder than it needs to be (read here), but that's also a necessary step.

In mocha, if you've got your code instrumented for coverage, the command for a travis build would look something like this:

YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha test -R mocha-lcov-reporter


With Mocha:

nyc --reporter=lcov mocha && codecov

With Jasmine:

istanbul cover ./node_modules/jasmine/bin/jasmine.js

With Karma:

The 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:

module.exports = function karmaConfig (config) {
        reporters: [
            // Reference:
            // Output code coverage files
        // Configure code coverage reporter
        coverageReporter: {
            reporters: [
                // generates ./coverage/
                {type:'lcovonly', subdir: '.'},
                // generates ./coverage/coverage-final.json
                {type:'json', subdir: '.'},

In package.json supply either or coverage-final.json to codecov:

  "scripts": {
    "report-coverage": "codecov",

Nodeunit + JSCoverage

Depend on nodeunit and jscoverage:

npm install nodeunit jscoverage codecov --save-dev

Add a codecov script to "scripts" in your package.json:

"scripts": {
  "test": "nodeunit test",
  "codecov": "jscoverage lib && YOURPACKAGE_COVERAGE=1 nodeunit --reporter=lcov test && codecov"

Ensure your app requires instrumented code when process.env.YOURPACKAGE_COVERAGE variable is defined.

Run your tests with a command like this:

npm run codecov

GitHub Actions

- name: Codecov
  uses: codecov/[email protected]
    token: ${{ secrets.CODECOV_TOKEN }}
    flags: unittests


Client-side JS code coverage using PhantomJS, Mocha and Blanket:

  • Configure Mocha for browser
  • Mark target script(s) with data-cover html-attribute
  • Run your tests with a command like this:
./node_modules/.bin/poncho -R lcov test/test.html && codecov


istanbul cover ./node_modules/lab/bin/lab --report lcovonly  -- -l  && codecov


  "scripts": {
    "report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",



jest --ci --coverage && codecov

or you can add it in your package.json:

"jest": {
  "coverageDirectory": "./coverage/",
  "collectCoverage": true

Jest will now generate coverage files into coverage/ and you can run your tests with a command like this:

jest && codecov


There have been reports of gotwarlost/istanbul not working properly with JSX files, which provide inaccurate coverage results. Please try using ambitioninc/babel-istanbul.


istanbul cover node_modules/.bin/tape ./test/*.js

Or simply run:

istanbul cover ./test/*.js

Or add in package.json:

"test": "istanbul cover test/*.js",

After test:

codecov --token=:token


Private Repo

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"

Common Pitfalls


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.



  1. More documentation at
  2. Configure codecov through the codecov.yml

We are happy to help if you have any questions. Please contact email our Support at [email protected]


FOSSA Status

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,539,835
Node (12,932
Related Projects