javascriptgruntjssonarqubemocha.js

import mocha unit tests result in sonarqube


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-runnerplugin 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 (e.g: 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)


Solution

  • 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'
           }
       },
    
    1. Run your test with 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