npm

Join us for the upcoming webinar, "Enterprise JavaScript done right: the tools you love, the security you need."Sign up here »

@danmasta/mocha-sonar

0.0.6 • Public • Published

Mocha Sonar Reporter

SonarQube XML reporter for Mocha

Features:

  • Generate xml reports compatible with SonarQube
  • Ability to also write to stdout
  • Ability to define custom reporter for stdout
  • Silent mode to disable output
  • Customize output file path/ name

About

We needed the ability to generate xml reports for unit tests and import them into SonarQube. There are a few libraries that attempt to help with this, but they all were either outdated, generated incompatible xml, or only logged xml to stdout. This library will let you generate unit test execution reports in a format compatible with sonarqube generic test format and write them to a file. You can also pipe output to a secondary reporter for a human readable view as well.

Usage

Add mocha-sonar as a dependency for your app and install via npm

npm install @danmasta/mocha-sonar --save-dev

Use the reporter for tests

mocha -R @danmasta/mocha-sonar tests

Options

Options can be passed using the reporterOptions field in gulp-mocha, or the --reporter-options field via command line

name description
cwd Location to use to generate relative file paths for tests. Default is process.cwd()
output File path where you would like the sonar xml report to be saved. Default is ./coverage/sonar.xml
delimiter Delimiter to use to join names for nested tests. Default is ' '
reporter Name of the reporter you would like to use for console output. Default is list
silent If true, disables console output
spaces Number of spaces to use when formatting xml output. Default is 4

Examples

Use mocha-sonar from command line

mocha ./tests/unit/**/*.js -R @danmasta/mocha-sonar

Use gulp, istanbul, and mocha to generate coverage and unit test reports for sonar

gulp.task('coverage:before', () => {
 
    return gulp.src([
        '*.js',
        'lib/**/*.js',
        'models/**/*.js',
        'routes/**/*.js'
    ])
        .pipe(istanbul({
            includeUntested: true
        }))
        .pipe(istanbul.hookRequire());
 
});
 
gulp.task('coverage', ['coverage:before'], () => {
 
    return gulp.src('tests/unit/**/*.js')
        .pipe(mocha({
            reporter: '@danmasta/mocha-sonar',
            reporterOptions: {
                reporter: 'list',
                output: './coverage/sonar.xml'
            }
        }))
        .pipe(istanbul.writeReports({
            dir: './coverage',
            reporters: [ 'lcov' ],
            reportOpts: { dir: './coverage' }
        }))
        .on('error', util.log)
        .once('end', () => {
            process.nextTick(process.exit);
        });
 
});

Contact

If you have any questions feel free to get in touch

install

npm i @danmasta/mocha-sonar

Downloadsweekly downloads

235

version

0.0.6

license

none

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability