I'm trying to get code coverage for unit tests in a Google Closure client-server project. We have code coverage for the server side, and need client side coverage.
JSCover runs its own server. Our cleint side unit tests require running under our server to access specific services. I don't see a way to make them work together, but a suggestion on how to do so would be ideal.
istanbul supports a number of underlying frameworks, but Google Closure does not appear to be one of them. Is there an easy way to use istanbul with a Google Closure unit test?
Blanket does not seem to be supported any longer. Does anyone have any recent experience that indicates it might still work with Google Closure?
Are there other coverage tools that would work well with Google Closure in a client-server configuration?
Istanbul works well with Google Closure and goog.testing.testSuite
, although it is not obvious how to set it up. In general, follow the instructions for using Istanbul with IoT.js.
More specifically, here's an outline of how we instrumented our own Google Closure tests with Istanbul to generate code coverage information:
Install Node.js.
Using the Node.js package manager, install the Istanbul command line tool using the command npm install --save-dev nyc
.
In our case, we use custom server code, so running our test suit under Node.js was not an option. I added a server-side call that accepted a file name and the contents of a file, and used this call to collect the code coverage statistics. If you don't need to use your own server code from your JavaScript tests, it is simpler to use Node.js as the server. See the link to using Istanbul with IoT.js (above) for some thoughts.
In each file for which code coverage is desired, run Istanbul's command line tool to instrument the file for coverage. The command will look something like nyc instrument myfile.js coverage_output_directory
. This changes your .js files, so be sure to have a copy somewhere that you can use to restore the file. I used a Python script to find an instrument the various files.
In each Google Closure test file, add this to the end of the file:
window.onbeforeunload = function( event ) {
/** @const {!FileUploadService} */
var fileUploadService = new FileUploadService( "../.." );
fileUploadService.upload( "coverage_output_directory.myfile.data", JSON.stringify( __coverage__ ) );
};
Use a unique output file name for each test file. FileUploadService
is the object we used to save result files on the server; you will need to replace this with your own service, or use the one in Node.js.
Run your tests.
Restore all changed files from your backups.
Use Istanbul and a report generator to create a code coverage report. For example, nyc report --reporter=lcov --temp-directory=coverage_output_directory
. This uses the lcov report generator, which installs with Istanbul and creates a nice report.
Inspect the code coverage using a browser by loading coverage_output_directory/lcov-report/index.html
.