Note: there has been an update. The pull request was rejected, but TJ Holowaychuck built a way to use custom reporters. See the end of the article for more information.
For unit testing, I usually use expresso. Expresso is a neat unit testing framework. It can even provide code coverage (a form of dynamic analysis) metrics. Expresso, however, seems to be no longer worked on. Instead, Mocha is the successor to expresso. Both are written by TJ Holowaychuck. Mocha is a more elaborate. For example, it provides several reporters which can be used to output the test results, and coverage details, to different formats.
Mocha has the xUnit reporter which saved the test results in a JUnit compliant format. Mocha does not have a reporter which writes the test results to the LCOV format. Therefore, I have written a new LCOV reporter for Mocha. It currently is a pull request, but hopefully it will be integrated in the Mocha tree soon.
First, lets install the new LCOV reporter for Mocha:
First, we need to store the results to a file. Doing so can easily be done without any modifications by running the command:
$ mocha -R xunit > TEST-all.xml
This results in a file called TEST-all.xml.
Second, we need to get the code coverage. Doing so is a bit harder, as we first need to ‘instrument’ the code to get the code coverage data. This is done using the node-jscoverage command. Then, we need to replace the original code by the instrumented code, and execute mocha with the new LCOV reporter. The following snippet shows this:
$ node-jscoverage lib lib-cov $ mv lib lib-orig $ mv lib-cov lib $ mocha -R lcov > coverage/coverage.lcov $ mocha -R xunit > coverage/TEST-all.xml $ rm -rf lib $ mv lib-orig lib
After executing this, we should have a file called coverage.lcov in the directory coverage, as well as a TEST-all.xml file. The file used can be found here.
After running the sonar-runner, you should have a project in Sonar, including test coverages screenshot for some an analysis on the MCO project.
Update: TJ Holowaychuck built a way to use custom reporters. At the time of writing, the HEAD-version in GitHub is not yet published on Npm, but this will probably be done soon. To use the LCOV reporter, install the mocha-lcov-reporter module through npm. Then, call Mocha as follows:
$ mocha --reporter mocha-lcov-reporter
This should give the coverage in the LCOV format.