Quantcast

JavaScript plugin: analyzing Mocha/JsCoverage projects

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JavaScript plugin: analyzing Mocha/JsCoverage projects

Steven Looman
Hi Users@Sonar,

On june 19th, Jean-Philippe Poulin de Courval sent an email regarding analysis of Mocha/JsCoverage projects. Being interested in this myself, I have adapted Mocha to be able to provide LCOV coverage data, the format used by the JavaScript plugin. See https://github.com/visionmedia/mocha/pull/485 for the pull request on GitHub.

As the pull request is not integrated (yet?), here is a short description on how use Sonar with the JavaScript plugin on your favorite JavaScript project. The full example-project can be found at https://github.com/StevenLooman/mco.

The JavaScript plugin expects test results in the xUnit format, with filenames using the convention TEST-<name>.xml. Mocha has an xUnit reporter, which we should use. Getting coverage data requires a bit more work. First, be sure to install the new LCOV reporter, currently available here: https://raw.github.com/StevenLooman/mocha/master/lib/reporters/lcov.js. I use the following script to get the test results in the xUnit format, and coverage data in the LCOV format:
--- snip ---
#!/usr/bin/env bash

rm -rf coverage
rm -rf lib-cov
mkdir coverage

echo Running mocha
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
--- snip ---

Finally, the Sonar/JavaScript plugin needs to know where to get the test/coverage data. This is done by setting the following properties:
sonar.javascript.jstestdriver.reportsfolder=coverage
sonar.javascript.jstestdriver.coveragefile=coverage.lcov
sonar.dynamicAnalysis=reuseReports

If all is set right, the JavaScript plugin will pick up the TEST-all.xml file for the test results, and the coverage.lcov file for the coverage data.

Regards,

Steven Looman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JavaScript plugin: analyzing Mocha/JsCoverage projects

Steven Looman
FYI,

The pull request was not integrated, but TJ Holowaychuck proposed a different solution: reporters can be loaded from other modules as well. At the moment the version of Mocha in Npm does not support this, but HEAD (GitHub) does.

I have put the LCOV reporter in a seperate module. Npm package: mocha-lcov-reporter. See https://github.com/StevenLooman/mocha-lcov-reporter. Replace the line:
mocha -R lcov > coverage/coverage.lcov
by
mocha -R mocha-lcov-reporter > coverage/coverage.lcov

Regards,

Steven

On Mon, Jul 2, 2012 at 10:37 PM, Steven Looman <[hidden email]> wrote:
Hi Users@Sonar,

On june 19th, Jean-Philippe Poulin de Courval sent an email regarding analysis of Mocha/JsCoverage projects. Being interested in this myself, I have adapted Mocha to be able to provide LCOV coverage data, the format used by the JavaScript plugin. See https://github.com/visionmedia/mocha/pull/485 for the pull request on GitHub.

As the pull request is not integrated (yet?), here is a short description on how use Sonar with the JavaScript plugin on your favorite JavaScript project. The full example-project can be found at https://github.com/StevenLooman/mco.

The JavaScript plugin expects test results in the xUnit format, with filenames using the convention TEST-<name>.xml. Mocha has an xUnit reporter, which we should use. Getting coverage data requires a bit more work. First, be sure to install the new LCOV reporter, currently available here: https://raw.github.com/StevenLooman/mocha/master/lib/reporters/lcov.js. I use the following script to get the test results in the xUnit format, and coverage data in the LCOV format:
--- snip ---
#!/usr/bin/env bash

rm -rf coverage
rm -rf lib-cov
mkdir coverage

echo Running mocha
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
--- snip ---

Finally, the Sonar/JavaScript plugin needs to know where to get the test/coverage data. This is done by setting the following properties:
sonar.javascript.jstestdriver.reportsfolder=coverage
sonar.javascript.jstestdriver.coveragefile=coverage.lcov
sonar.dynamicAnalysis=reuseReports

If all is set right, the JavaScript plugin will pick up the TEST-all.xml file for the test results, and the coverage.lcov file for the coverage data.

Regards,

Steven Looman

Loading...