Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll set up Istanbul in a simple calculator project.
Resources
Code Downloads
git clone https://github.com/treehouse-projects/calculator.js-code-coverage calculator.js
Commands
npm install istanbul --save-dev
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha
If you're using a more complex testing directory with sub-folders you should use the recursive
flag like this:
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -recursive
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
If you haven't cleared the repository,
0:00
do that now or
download the zip file with this video.
0:02
Change into the directory and install
the current development dependencies.
0:15
Npm install should install
the testing framework Mocha and
0:23
the assertion library Chai.
0:27
The calculative functionality
is in the index.js file.
0:32
There's functions for
addition, subtraction.
0:39
Division and multiplication,
they're all exported at the bottom here.
0:43
Mocha is being called from
the package.json file.
0:52
So we can run npm test.
0:55
There's currently three pending tests for
the multiplication function.
1:03
Let's install Istanbul as
a developer dependency.
1:08
To run Istanbul,
1:22
you can run the Istanbul binary
1:25
followed by the cover argument.
1:30
And the mocha binary.
1:39
You have to use the _mocha file
because the mocha binary folks and
1:42
executes this under the hood.
1:47
This way Istanbul can hook
into the underlying code.
1:49
A link in the teacher's note
covers the GitHub issue that
1:53
covers why you'd want to include
the _mocha rather than mocha.
1:56
Once you've run Istanbul,
2:01
if you look in the root of the folder,
you'll see a coverage directory.
2:03
And the coverage folder is
where HTML reports are.
2:09
Now that we know that
the Istanbul binary works,
2:12
let's modify the package.json
file to have a coverage task.
2:15
I'm going to call this task test:coverage.
2:21
This is because I'm still running
the tests, but with the coverage.
2:30
We don't need to include
the full binary paths for
2:37
each executable since NPM
knows where the binaries are.
2:39
So we can write the tasks simply
by saying, istanbul cover _marker.
2:44
Save the package.json file and then go
back to the terminal to see if it works.
2:50
Awesome, it does work.
3:04
Finally, you don't want to check
your coverage into version control.
3:08
You can modify the .gitignore file to
include the coverage directory too.
3:12
In the next video,
we'll take a look at the report.
3:18
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up