npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

karma-sonarqube-generic-reporter

v1.0.0

Published

A Karma plugin. Report results in xml format for SonarQube.

Downloads

30

Readme

karma-sonarqube-generic-reporter

Reporter for the SonarQube Generic Test Data XML format.

Based on the original karma-junit-reporter.

This version is adjusted to work with SonarQube 7.x and the Generic Test Data importer used by SQ to import unit tests.

Installation

The easiest way is to keep karma-sonarqube-generic-reporter as a devDependency in your package.json. Just run

npm install --save-dev https://github.com/tobiashofmann/karma-junit-reporter#master 

to let npm automatically add it there. This adds the latest version of this code to your package.json:

"karma-junit-reporter": "https://github.com/tobiashofmann/karma-junit-reporter#master",

Example package.json

{
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "devDependencies": {
    "karma": "^2.0.2",
    "karma-chrome-launcher": "^2.2.0",
    "karma-coverage": "^1.1.2",
    "karma-coverage-istanbul-reporter": "^2.0.1",
    "karma-qunit": "^1.2.1",
    "karma-sinon": "^1.0.5",
    "karma-sonarqube-generic-reporter": "https://github.com/tobiashofmann/karma-sonarqube-generic-reporter/#master",
    "qunitjs": "^2.0.0"
  },
  "main": "test.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "MIT",
  "description": "",
  "dependencies": {}
}

Configuration

The configuration is slightly different from the karma.junit.reporter as I removed some not needed parameters.

// karma.conf.js
module.exports = function(config) {
  config.set({
    reporters: ['progress', 'junit'],

    // the default configuration
    junitReporter: {
      outputDir: '../reports/junit', // results will be saved as $outputDir/$browserName.xml
      outputFile: undefined, // if included, results will be saved as $outputDir/$browserName/$outputFile
      useBrowserName: false // add browser name to report and classes names
    },
    }
  });
};

Example

To check whether the karma plugin is working I use my own Wake On Lan app written in OpenUI5. The source code is available on GitHub too: wol-ui5 The HTML app is in the folder frontend. After getting the source code, change to the folder and run npm install and then karma start.

git clone https://github.com/tobiashofmann/wol-ui5
cd wol-ui5
cd frontend
nom install

Run karma

To generate the report file, run karma. The plugin will create an XML file in the specified location. For instance, at ./reports/junit.

report file location

The content of the file will be scanned by SonarQube and must match their definition. An example XML file content generated by the plugin can be seen here:

sample xml file content

This unit test result contains one failed test. Test ServerModel failed and contains a sub element named .

Upload report to SonarQube

For SonarQube to pick up and analyse the content of the report file, you need to inform the sonar scanner (or whatever tool you use) about the location of the XML file. To do so, edit the sonarqube.properties file.

# coverage reporting
sonar.testExecutionReportPaths=reports/junit/TESTS.xml

To upload the report, execute an analysis of the project. Using sonar-scanner:

sonar-scanner

Output:

INFO: Scanner configuration file: /users/dev/tools/sonar-scanner/conf/sonar-scanner.properties
INFO: SonarQube server 7.0.0
...
INFO: Sensor Generic Test Executions Report
WARN: Property 'sonar.genericcoverage.unitTestReportPaths' is deprecated. Please use 'sonar.testExecutionReportPaths' instead.
INFO: Parsing /Users/wake-on-lan/frontend/reports/junit/TESTS.xml
INFO: Imported test execution data for 5 files
INFO: Sensor Generic Test Executions Report (done) | time=76ms
...
INFO: Sensor SonarJS Coverage [javascript]
INFO: Analysing [/Users/wake-on-lan/frontend/reports/coverage/lcov.info]
INFO: Sensor SonarJS Coverage [javascript] (done) | time=12ms
...
INFO: Task total time: 12.664 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------

The scanner is pickung up hte TESTS.xml file from the specified location and imports the test data for all 5 test files. In case the location of the file specified in the XML report by is invalid, sonar-scanner will show you the file not found.

SonarQube

With the successfull import done, you can see the unit test results in SonarQube.

unit tests overview

The unit tests are listed in the code measure section.

unit tests code measure

The test file contains additional information about the unit tests. For a successfull test:

successfull unit test

In case of a not successfull unit test, SonarQube can show the additional information on the error as provided by the element in the XML report.

error unit test