Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rbournissent/0a704cae6347bbda460c to your computer and use it in GitHub Desktop.
Save rbournissent/0a704cae6347bbda460c to your computer and use it in GitHub Desktop.
How to integrate TravisCI and CodeClimate with your NodeJS project

Integrate TravisCI and CodeClimate with a NodeJS app

First Steps

Add your repo to TravisCI

  1. Go to https://travis-ci.org/, and add your GitHub repo.
  2. From the Repo settings, on Travis, you can set the ENV vars and enable some options such as
  • Build on push
  • Build pull requests

Add your repo to CodeClimate

  1. Go to https://codeclimate.com/dashboard and add your GitHub repo
  2. Then go to Settings (you'll need admin rights to do this step) and then to the Test Coverage tab. Scroll down and click on Travis button, copy the code to include on your travis.yml

Create .travis.yml

  1. Create a .travis.yml file on your project folder
  2. Let's use this example to understand the file structure:
language: node_js
node_js:
  - "5.5.0"
addons:
    code_climate:
        repo_token: <supersecrettoken>
script: istanbul cover node_modules/mocha/bin/_mocha -- -R spec
before_script:
  - npm install codeclimate-test-reporter istanbul -g
after_script:
  - codeclimate-test-reporter < ./coverage/lcov.info
  • language: specify the language of your project
  • node_js: you can specify the (minimun) version to use your app
  • addons: this is what you should copy from code_climate, including repo_token and the token itself
  • script: specify the command you use to run your tests. We are using istanbul to generate the file formatted as lcov to send it to climate. A more generic command could be: istanbul cover [npm command to run the tests. e.g.: test] -- -R spec, in the example above I'm using mocha
  • before_script: the commands we need to run before run the script, so we need to install codeclimate-test-reporter and istanbul, globally
  • after_script: the commands we need to run after run the script (good names, heh?). In this case we have to send to CodeClimate the report generated by istanbul which is in coverage folder.
  1. Encrypt repo_token (recommended)
  2. Install travis: gem install travis
  3. run travis encrypt <supersecrettoken>
  4. Copy the output in replace of supersecrettoken. Your addons section will be like this:
addons:
  code_climate:
      repo_token:
        secure: "veryverylongstringhere"

You are ready

Now you just have to commit the .travis.yml file and see Travis do his job

Additional steps (optional)

  1. Add some cool badges to your README.md on GitHub
  • Codeclimate provides badges with CodeClimate score and Test Coverage
  • Travis provides a badge with the build status (failing or passing)
  1. Add coverage/ folder to .gitignore
@chandu1310
Copy link

Thanks for this helpful information.
Since istanbul is deprecated, and is replaced by nyc, here is the modified script I used after referring to this amazing article.
This generated code coverage for code which can be in ES6

language: node_js
node_js:
  - "8"
addons:
    code_climate:
        repo_token:
           secure: supersecuretokenencrypted
script: ./node_modules/.bin/nyc --reporter=lcov ./node_modules/mocha/bin/mocha --require @babel/register
before_script:
  - npm install codeclimate-test-reporter nyc -g
after_script:
  - codeclimate-test-reporter < ./coverage/lcov.info

@f9mac
Copy link

f9mac commented Jul 24, 2019

Thank you guys for providing that info.

Since codeclimate-test-reporter is deprecated now too, here is a modified script I used with the new cc-test-reporter binary

env:
  global:
    - CC_TEST_REPORTER_ID=XXXXXXXXXXXXXXX
language: node_js
node_js:
  - '8'
before_script:
  - npm install
  - npm install nyc -g
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
  - chmod +x ./cc-test-reporter
  - ./cc-test-reporter before-build
script: npm test
after_script:
  - ./cc-test-reporter after-build -t lcov --exit-code $TRAVIS_TEST_RESULT

@fabzer0
Copy link

fabzer0 commented Aug 1, 2019

Hey @f9mac is the CC_TEST_REPORTER_ID the repo secure token?

@chandu1310
Copy link

language: node_js
node_js:
- '8'
cache:
  directories:
  - node_modules
install:
- npm install nyc -g
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
script:
- nyc --reporter=lcov npm run test
after_script:
- ./cc-test-reporter after-build

Some more changes to isolate CC_TEST_REPORTER_ID from the yml file.
CC_TEST_REPORTER_ID can be provided as a secure environment variable in travis by going to the More Options >> Settings for the repo. This avoid checking in secure code to public repos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment