I use mocha to get unit tests results and istanbul to get code coverage. I'm using grunt to run these tasks. It works fine. I'm also using grunt-sonar-runner
plugin to import these result in sonar. Currently code coverage is imported but that is not the case for unit tests results. During the build, sonar report me this :
20:40:19.410 WARN - Test result will not be saved for test class "Account Controllers User Controller forgot password", because SonarQube associated resource has not been found using file name: "Account Controllers User Controller forgot password.js"
20:40:19.411 WARN - Test result will not be saved for test class "Account Controllers User Controller login", because SonarQube associated resource has not been found using file name: "Account Controllers User Controller login.js"
20:40:19.413 WARN - Test result will not be saved for test class "Account Controllers User Controller logout", because SonarQube associated resource has not been found using file name: "Account Controllers User Controller logout.js"
Because of this, sonar don't save unit tests results. I tryed to change the javascript plugin version in 2.2, or upgrade sonar system in 5.1.1 but the problem is the same. I also try to rename all describe
function to form the right path to the file with .
between folders (eg: test.unit.controllers.filename.
And I realized that it works for only one test. If you have more than 1 tests, it will not work.
configuration:
* sonar (4.5.2)
* javascript plugin (2.7)
npm modules:
* mocha-sonar-reporter (^0.1.3)
* mocha: (^2.1.0)
* grunt-mocha-test (^0.12.7)
I actually get code coverage with istanbul and unit tests results with mocha. Indeed, the mocha (xunit reporter) fails. It only works if you have one test and a classname correctly set with .
between folders and filename. Here is the solution with mocha and grunt, please follow exactly these steps and respect naming of files and folders :
1. Use npm module : sonar-mocha-reporter
2. Add these lines in your package.json:
"config": {
"mocha-sonar-reporter": {
"classname": "Test",
"testdir": "test",
"outputfile": "report/TEST-results.xml"
}
}
3. Define npm test cmd (I defined a grunt task to run test with grunt mocha-test
plugin ):
"scripts": {
"test": "grunt runTests"
}
4. Define the grunt task:
module.exports = function(grunt) {
grunt.config.set('mochaTest', {
test: {
options: {
timeout: 6000,
reporter: 'mocha-sonar-reporter',
quiet: false,
clearRequireCache: true
},
src: ['test/**/*.js']
}
});
grunt.loadNpmTasks('grunt-mocha-test');
};
5. Configure your reporting with grunt sonar-runner
plugin and add these lines if it not already the case (options object):
dynamicAnalysis: 'reuseReports',
tests: 'test',
javascript: {
jstestdriver: {
reportsPath: 'report'
},
lcov: {
reportPath: 'report/lcov.info'
}
},
npm test
command. with grunt
or gulp
, it won't work. configuration:
* sonar (4.5.2)
* javascript plugin (2.7)
npm modules:
* mocha-sonar-reporter (^0.1.3)
* mocha: (^2.1.0)
* grunt-mocha-test (^0.12.7)
Useful documentation which helped me to solve this problem : sonarqube javascript plugin docs
In a node app I solve this problem doing the steps bellow:
First: In package.json file, I included this line:
"scripts": {
...,
...,
"test": "nyc --reporter=lcov --reporter=text-lcov mocha test/*/*.js"
},
Second: Install nyc
npm install nyc --save-dev
Last: Run sonar-scanner
I created an example in a repo for this case:
https://github.com/macielbombonato/docker-sonar/blob/master/tools/scripts/scanner-npm.sh
I hope this can help.
I am using using Bamboo for CI + SonarQube, so this answer (for NodeJS) combines both, to have visibility in Bamboo for test reports and in SonarQube for test execution visibility and code coverage visibility:
yarn add -D mocha-bamboo-reporter mocha-multi-reporters mocha-sonarqube-reporter
sonar-project.properties
:
# this is for code coverage
sonar.javascript.lcov.reportPaths=coverage/lcov.info
# this is for test reports
sonar.testExecutionReportPaths=coverage/test_results.xml
package.json
: "testWithCoverage": "nyc -r lcov -e .ts -x \"*.test.ts\" mocha --reporter mocha-multi-reporters --reporter-options configFile=mocha-multi-config.json -r ts-node/register test/**/*.spec.ts && nyc report",
mocha-multi-config.json
: {
"reporterEnabled": "mocha-bamboo-reporter, mocha-sonarqube-reporter",
"mochaSonarqubeReporterReporterOptions": {
"output": "coverage/test_results.xml"
},
"mochaBambooReporterReporterOptions": {
"output": "coverage/mocha.json"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.