Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage using Istanbul #44

Closed
riteshe63 opened this issue Sep 28, 2016 · 5 comments
Closed

Code coverage using Istanbul #44

riteshe63 opened this issue Sep 28, 2016 · 5 comments
Assignees
Labels

Comments

@riteshe63
Copy link
Contributor

riteshe63 commented Sep 28, 2016

Istanbul is a code coverage tool for JavaScript.

To run Istanbul with Mocha, you can use this command:

npm install istanbul
./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- -R spec

node_modules/.bin/istanbul test runs the istanbul executable with the test command.

node_modules/.bin/_mocha points to the mocha executable which is how my tests are run.

-R spec are my arguments for mocha.

-- tells istanbul to pass the following arguments the test process rather than consume them itself.

Mocha users, note the _mocha executable path contains an underscore. This is because the executable without an underscore forks processes to run your tests.

You can now run your tests with:

npm test
Istanbul just runs your tests for you without instrumenting.

And generate a coverage report with:

npm test --coverage
You will get some terminal output, with any luck 100% coverage like module:

===== Coverage summary ======
Statements : 100% ( 64/64 )
Branches : 100% ( 30/30 )
Functions : 100% ( 13/13 )
Lines : 100% ( 58/58 )
============= ================
…and an html coverage report at ./coverage

@gaurav21r
Copy link
Member

@riteshe63 looks good but a few points.

  1. How do we instrument front end tests? We include mocha in HTML but don't write the command.
  2. npm test will not work without a valid entry in package.json's scripts:. Please revisit your source and add relevant details
  3. Pl research if all this can be done with grunt ?
  4. Where is the codecov report created? How do we specify the path where it should be created? Where should it be created?

@riteshe63
Copy link
Contributor Author

Sorry some point were missing previously. Those point have been resolved and included here:

  1. ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -R <folder name for test>
    (doubtful this point)
  2. Yes, npm test will not work without a vaild entry in package.json's scripts:. We need to include a line command inside the scripts: tag :
    "test": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- -R spec"
    After that run npm test and it will work properly.
  3. Research still continues if all this can be done with grunt.
  4. By default codecov reporter is not present. So we need to install codecov first. After that we use reporter like:
    npm install codecov.io
    To run codecov repoter with mocha, we use following command:
    ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec ./node_modules/.bin/codecov
    And path for the reporter where it will be saved is:
    project/coverage/lcov-report/index.html

@gaurav21r
Copy link
Member

@riteshe63 for 2. We will use grunt instead of npm scripts so the main this is 3. where we have to figure out how to integrate everything with Grunt.

Regarding 4, By codecov I meant Code Coverage report which will simply tell us which lines of code were tested and which weren't. codecov itself is a service integration under http://codecov.io so I don't think we need that at this point. We only need to generate lcov-report

@riteshe63
Copy link
Contributor Author

Regarding the 2nd point, we will run the code coverage command mentioned below without scripts
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec

For the 4th point, When we run the above mentioned command then it will create a coverage folder in root project by default. Inside this, there will be lcov-report.
In several projects as per my research, coverage folder is usually present inside it .gitignore. We can look at some of the references like:
https://github.com/serverless/serverless/blob/master/.gitignore
https://github.com/bower/bower/blob/master/.gitignore
https://github.com/expressjs/express/blob/master/.gitignore
https://github.com/slap-editor/slap/blob/master/.gitignore
https://github.com/jshint/jshint/blob/master/.gitignore
https://github.com/yaniswang/HTMLHint/blob/master/.gitignore
https://github.com/TryGhost/Ghost/blob/master/.gitignore
https://github.com/balderdashy/sails/blob/master/.gitignore

@gaurav21r
Copy link
Member

gaurav21r commented Nov 21, 2016

@riteshe63 Everything looks good, just let me know about front end tests. Continued in #74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants