Skip to content

Instantly share code, notes, and snippets.

@arrested-developer
Created September 4, 2018 09:20
Show Gist options
  • Save arrested-developer/6432929e6093964a58c2bd5c45dc4df7 to your computer and use it in GitHub Desktop.
Save arrested-developer/6432929e6093964a58c2bd5c45dc4df7 to your computer and use it in GitHub Desktop.
Node.js / Travis / Codecov CI Cheatsheet

Node CI Cheatsheet

Because you're super cool and you want a quick reference to automate / monitor your tests with Travis and Codecov 😎

There are other testing modules out there, but this guide will focus on Tape, Tap-Spec and NYC.

This guide assumes that you already have a repo set up and ready to use. If you don't, do it now!

1. Sign up to Travis and Codecov

Go to travis-ci.org and sign in with Github, give the necessary permissions to Travis and check that you've added any organisations whose repos you need Travis to access. You might need to get the organisation's owner to give authorisation.

Go to codecov.io and sign in with Github, give the necessary permissions.

2. Activate Travis for your repo

Once Travis has synced with your Github profile, you should see all your repos in your Travis profile page. Find the repo you want to activate and toggle the switch next to the repo name. You'll need admin access for any repo you want to use with Travis.

3. Install npm modules

If you haven't yet, run npm init on your project folder.

After your repo is initialised, you can run:

npm install tape tap-spec nyc codecov --save-dev

4. Add test scripts to package.JSON

"scripts": {
    "test": "nyc tape ./test/*.js | tap-spec",
    "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
  },

Note: adding NYC to the start of your test script will add a coverage check to your tests. The coverage script then takes the report that NYC generates and uploads it to Codecov. 🎉

Another note: You can, of course, use other testing modules than Tape and NYC - you'll need to adjust these scripts accordingly.

5. Add .travis.yml file

Create the file in the root of your project and add the following:

language: node_js
node_js :
 - "node"
after_success: npm run coverage
notifications:
  email:
    on_success: never
    on_failure: never

This will set up Travis to run the latest version of Node and while we're at it we can also suppress the endless stream of email notifications. If you like getting emails, just change never to always 💌

The 'after success' line will tell Travis to run our coverage script after it has completed the test script AND this will automatically send your test coverage stats to Codecov. 💅

6. Make sure you have tests!

Travis doesn't really have much to show you unless you have tests set up.

The script we set up earlier will be looking for test scripts in the test folder of your repo. Any JS file in this folder will be run, so be sure to only put your tests in there. You can have as many separate test files as you want, e.g. logic.test.js, router.test.js - it's a good idea to keep your tests in several files instead of cramming them all into one.

When you require one of your modules in a test file, make sure you point to the folder it's in, e.g.

const myModule = require('../public/mymodule');

To get started, you can create the file initial.test.js (don't forget to put it in your test folder) and add the following:

const tape = require('tape');

tape('check tape is working', (t) => {
  const expected = 2;
  t.equal(1 + 1, expected, '2 should equal 2');
  t.end();
});

7. Commit your changes

Add and commit your changes to your repo. Push it all up to the master branch, because you love danger. Seriously though, you're just initialising the repo for your team so it's ok to set it up on the master branch, just make sure you're the only one pushing changes to it.

8. Watch Travis and Codecov get to work

As soon as Travis spots the .travis.yml file in your repo it will get to work. Travis clones your repo, sets up a Node environment and runs all your tests. We also set up Codecov, so when Travis has finished it will send a coverage report over to Codecov! View your repo on Codecov to see some cool graphs and charts of your coverage stats.

Note: If you've followed all these steps and Travis hasn't started doing its thing after a few minutes, try making a small change and doing another commit.

9. Add cool badges to your readme!

Get your Travis badge at:

https://api.travis-ci.org/GITHUBUSERNAME/REPONAME?branch=master

Go to codecov.io/gh/GITHUBUSERNAME/REPONAME/settings/badge to see your coverage badge options

10. Bonus Step: Set up continuous deployment

Thanks to Kate SB for these! 🎉

Hi all - if you want to set up continuous deployment on Heroku, follow these steps!

  1. Make sure that the person who initiates the Heroku app on their account has admin access to the repository on Github
  2. Go to Heroku, click through to your app, and go to the Deploy tab
  3. Scroll down to Deployment Method, and change it from Heroku Git to Github
  4. You’ll be prompted to authorise and log in with Github
  5. Search for your repo name. If you have your repo on your own account, you should be able to find it straight away. If it’s on FAC-14, you might need to request access. Click the repo to connect.
  6. A new section called Automatic Deploys should appear. Choose Master as the branch to continuously deploy from.
  7. Tick the box for ‘Wait for CI to pass before deploy’ if you have CI like Travis set up
  8. Click enable automatic deploys
  9. Party! 🎉

Troubleshooting

Travis isn't recognising my repo / branch

Travis can be very sensitive about the .travis.yml file - check for any trailing spaces in your commands

I can't find my repo on Travis / Codecov! 🔍

Travis and Codecov URLs follow a similar pattern to Github URLs

For repos in your own Github

travis-ci.org/GITHUBUSERNAME/REPONAME codecov.io/gh/GITHUBUSERNAME/REPONAME

For repos in an organisation's Github

travis-ci.org/GITHUBORGANISATION/REPONAME codecov.io/gh/GITHUBORGANISATION/REPONAME

A test is passing locally but failing on Travis 😢

Check to see if your test depends on loading a file with fs - Travis has a case-sensitive filesystem so you need to type the filename precisely (e.g. picture.jpeg is not the same as picture.JPEG)

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