Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get karma javascript junit xml reports into sonar

Is there a karma reporter which can be used to generate a results.xml file which is compatible with the sonar Generic Test Coverage plugin? Or alternatively a parser for the junit file which is output by karma-junit-reporter and which will work with the generic plugin?

I am using SonarQube 5.3 and Karma / Jasmine

I want to import the junit reports that come from karma into SonarQube but cannot see a way to do this easily. Something like https://www.npmjs.com/package/mocha-sonar-generic-test-coverage for karma

FYI I have seen karma-junit-sonarqube-reporter (https://www.npmjs.com/package/karma-junit-sonarqube-reporter) but that seems to expect the name of the test to match the path to the file which is too restrictive for me. karma-sonarqube-unit-reporter seems to be unfinished grunt-karma-sonar seems to rely on jstestdriver which is deprecated

like image 200
Kevin McDonnell Avatar asked Feb 05 '16 01:02

Kevin McDonnell


1 Answers

I managed to achieve this by doing the following.

To get javascript junit reports into sonar

  1. find and download sonar-karma-test-report-plugin-1.0.0.4.jar which is not available via the update center
  2. put jar file in /usr/local/Cellar/sonar/5.3/libexec/extensions/plugins and restart sonar
  3. Tell karma to omit the name of the browser

    junitReporter: {
           useBrowserName: false 
    }
    
  4. Get Karma to create a junit xml file via the normal karma-junit-reporter and have it call the file TESTS-xunit.xml (I had the file called test-results.xml and sonar would not detect it WTF)

  5. In the sonar-project.properties file set sonar.javascript.karmajstestdriver.reportsPath=reports/js/unit-components/results/

  6. call sonar-runner

Sonar requires the path to the test file in order to process the report. Jasmine does not make this available to the reporter. My understanding is the sonar plugin iterates over xml file and finds the classname for each test which has the test name (ie my cool tests) and does a string replace to change that to the location of the file

like image 76
Kevin McDonnell Avatar answered Sep 28 '22 21:09

Kevin McDonnell