A transcendent blend of useful JS unit testing and colourful coverage graphs.
Coverage is provided by the jscoverage project.
Rails 4 is not yet supported.
First, make sure you have jscoverage and qtmake installed. Qtmake is required by a gem we depend on called jasmine-headless-webkit. Eg on Linux:
sudo apt-get install jscoverage libqtwebkit-dev qt4-dev-tools
Then add jasmine-coverage to your Gemfile
Finally, add this to your Rakefile
Note, the requirement to have the git branch of the jasmine-headless-webkit gem is no longer necessary.
To use jasmine-coverage, run the rake task.
bundle exec rake jasmine:coverage
bundle exec rake jasmine:coverage JASMINE_CONFIG='path/to/jasmine.yml'
You will see the tests execute, then a large blob of text, and finally a summary of the test coverage results. An HTML file will also be saved that lets you view the results graphically, but only if served up from a server, not local disk. This is because the jscoverage generated report page needs to make a request for a local json file, and browsers won't allow a local file to read another local file off disk.
To reiterate: if you try to open the report file locally, you will see NETWORK_ERR: XMLHttpRequest Exception, as the browser may not access the json file locally. However if your build server allows you to browse project build artefacts, you can view the visual report as the json is served from there too.
Files generated will be
target/jscoverage/jscoverage.html - The visual report shell target/jscoverage/jscoverage.json - The report data target/jscoverage/jscoverage-test-rig.html - The actual page that the tests executed in
You can set a failure level percentage.
bundle exec rake jasmine:coverage JASMINE_COVERAGE_MINIMUM=75
bundle exec rake jasmine:coverage JASMINE_COVERAGE_KEEP_TEST_RIG=false
You can also specify if you want missing coverage warnings
bundle exec rake jasmine:coverage JASMINE_COVERAGE_WARNINGS=true
How it works
The data we get from the coverage can only "leave" the JS sandbox one way: via the console. This is why you see such a large block of Base64 encoded rubbish flying past as the build progresses. The console data is captured by Jasmine Coverage, which decodes it and builds the results HTML page, and gives a short summary in the console.
The tool uses the headless gem, which needs to create an X virtual frame buffer. Linux machines should work out the box, but since Mac uses Quartz, you may get
Xvfb not found on your system
To solve that just install XQuartz and add /usr/X11/bin to your PATH (thanks @shell).
Thanks to the following for comments and pull requests: