I like to use Mocha with the BDD test UI to write my tests and in the office at least, use Sonar to collect test and coverage data as well as perform static analysis on source code. For Javascript projects this means using the Sonar javascript plugin.

A typical sonar-project.properties configuration file will look like this.

sonar.projectName=My Application



This tells Sonar that my test results will be in the reports directory and that coverage data will be located at reports/lcov.info

The Sonar javascript plugin supports coverage data in the lcov format and test reports in the xunit format.

For my NodeJS projects I use my grunt-mocha-test plugin to generate the reports. In this case we can use the mocha-lcov-reporter to generate the coverage report. However when using the standard xunit reporter to generate the test report 2 problems become apparent when the reports are submitted to Sonar.

The solution for me was to create a new reporter for Mocha based on the xunit reporter.

mocha-sonar-reporter will generate xunit output that uses the concatenation of the suite and test titles as the test name and set the classname to a configurable constant so that name collisions can be avoided. If no classname is configured it will default to Test.

comments powered by Disqus