Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run code coverage for client side javascript code using grunt

How can I run code coverage for client side javascript code using grunt?

I have run the client side javascript code test using mocha-phantomjs to be tested using html page, and it works fine, but I couldn't find a way to run code coverage for it.

It will be great if there is some sample code.

like image 840
Shadi Abu Hilal Avatar asked Nov 01 '22 06:11

Shadi Abu Hilal


1 Answers

I have found away to do it using mocha phantomjs istanbul and browserify through grun.

In the client JS source code, make sure to expose your public var(s) or function(s) though window object, to make it accessible after generating the instrumentation file by browserify. here is an example: https://github.com/shadiabuhilal/js-code-coverage-example/blob/master/src/example.js#L36

devDependencies:

"browserify-istanbul": "^0.2.1",
"chai": "^1.10.0",
"grunt": "^0.4.5",
"grunt-browserify": "^3.7.0",
"grunt-cli": "^0.1.13",
"grunt-contrib-clean": "^0.6.0",
"grunt-mocha-phantom-istanbul": "^0.2.1",
"mocha": "^2.1.0"

To see full example please check the following repo:

https://github.com/shadiabuhilal/js-code-coverage-example

like image 171
Shadi Abu Hilal Avatar answered Nov 12 '22 15:11

Shadi Abu Hilal