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!
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.
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.
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
"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.
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. ๐
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();
});
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.
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.
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
Thanks to Kate SB for these! ๐
Hi all - if you want to set up continuous deployment on Heroku, follow these steps!
- Make sure that the person who initiates the Heroku app on their account has admin access to the repository on Github
- Go to Heroku, click through to your app, and go to the Deploy tab
- Scroll down to Deployment Method, and change it from Heroku Git to Github
- Youโll be prompted to authorise and log in with Github
- 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.
- A new section called Automatic Deploys should appear. Choose Master as the branch to continuously deploy from.
- Tick the box for โWait for CI to pass before deployโ if you have CI like Travis set up
- Click enable automatic deploys
- Party! ๐
Travis can be very sensitive about the .travis.yml file - check for any trailing spaces in your commands
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
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)