These are my notes based on the phenomenal tutorial by Kent C. Dodds hosted by egghead.io both of which are my go to sources when I want to learn something new. I highly suggest you sign up for the pro subscription.
Part 1 | Part 2 | Part 3 | Part 4 | >Part 5 | Part 6 | Part 7
Part 5: Automating Testing and Code Coverage
Automatically running tests before commits with ghooks
I really think git hooks are pretty sweet! So git has events right? commit, push, receive etc. Well what if you wanted to do stuff before you committed or after a push? Well ghooks is how you do that.
read more at git-scm.com , http://githooks.com/ or on npm
Let’s get started by installing it as a dev dependency for this project. You don’t want to install it globally btw as it can cause problems. Head to your console and type:
$ npm i -D ghooks
Now we need to configure ghooks to run our test before every commit. This is pretty easy. Go to package.json and add:
"config": { "ghooks": { "pre-commit": "npm run test:single" } }
Now head into your test file and break the test by making it fail, then run:
$ git commit -am "Testing ghooks"
It should have prevented the commit, so go back and fix the test. Then let’s see it work on when we run our commit script.
$ git status $ git diff $ git add . $ git status $ npm run commit
Great! Now that we have automated the testing process a bit how about taking a look at how much of our code is covered by our tests? How would we do this? There are several ways but I am most comfortable with Istanbul, it has great docs and is pretty widely used.
Adding code coverage with Istanbul
Install istanbul as a dev dependency:
$ npm i -D istanbul
Verify in package.json that istanbul was added to the devDependencies section. Now we need to adjust the test scripts. Change test:single to:
“test:single”: “istanbul cover -x *.test.js _mocha -- -R spec src/index.test.js”
What is happening there? Well first we are firing off the istanbul binary and passing in cover. This passes in the cover command and saves coverage.json and reports after execution. Next we see -x to ignore checking the coverage ON our test files since we are not writing tests for tests. The _mocha tells istanbul that the next flags passed in should go to mocha and not to istanbul. Now run our test and see the difference now:
$ npm run test:single
Pretty neat eh! Let’s make it even cooler.
Adding Code Coverage Checking
open package.json and set new ghooks to:
"config": { "ghooks": { "pre-commit": "npm run cover && npm run check-coverage" }
Go back up to scripts in package.json and add:
“check-coverage”: “istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100”
The –name value above set’s what percentage we want our code to pass before we let it go forward. Since this is a small project 100 is a good bet. To see what this does run:
$ npm run check-coverage
Now add a new function to your index.js and run tests again. I simply created a dummy function:
function notCovered() { return 'oops'; }
$ npm run test: single
This will let us check the coverage of that specific test allowing us to re-run check-coverage:
$ npm run check-coverage
Now since we are all about the automation we want to add this to Travis so open .travis.yml and add to script: section
- npm run check-coverage
Sweet! Now remove that bunk function before we forget. Now we are going to add codecove.io report viewing to the project. I mean why not right?
Go to codecov.io and sign up for an account. Integrate your repo for the npm module. The steps are pretty straight forward. Now go to the terminal and from within the project directory run:
$ npm i -D codecov.io
Then go to package.json we will pipe a report into the codecov binary by adding a new script to package.json:
“report-coverage”: “cat ./coverage/lcov.info | codecov”
Now on to .travis.yml (this pattern is getting familiar right?)
after_success: - npm run test report-coverage
$ git status $ git add . $ npm run commit $ git push
Check code coverage at codecov.io. Optionally you can add a chrome extension for codecov here to see more integration on github.com.
Now wasn’t that easy!
Adding badges to the Readme
Head to http://shields.io and search for Travis copy url into README.md then edit the url to be your repo. You can also look at the styles at the bottom if you want to tweak the shield. Do the same for:
- codecov
- npm version
- npm downloads
- npm license
$ git status $ git add . $ npm run commit $ git push
Key:
= Time Saving Idea
= Pro Tip
= Note
= Alert
= Code Update
= A Closer Look