@danmasta/mocha-sonar

中文文档
1.1.0发布于 2024/01/12MITCommonJS近 30 天下载 2999

Mocha reporter for Sonarqube

原始使用文档
文档目录5 节
  1. About
  2. Usage
  3. Options
  4. Examples
  5. Contact

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 and/or name

About

We needed the ability to generate xml reports for unit tests and import them to 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 mocha options, or the --reporter-options field via command line

namedescription
cwdLocation to use to generate relative file paths for tests. Default is process.cwd()
outputFile path where you would like the sonar xml report to be saved. Default is ./coverage/sonar.xml
delimiterDelimiter to use to join names for nested tests. Default is ' '
reporterName of the reporter you would like to use for console output. Default is list
silentIf true disables secondary reporter output. Default is false
spacesNumber of spaces to use when formatting xml output. Default is 4

Examples

Use mocha-sonar from command line

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

Pass options to npm test command

npm run test -- --reporter=@danmasta/mocha-sonar --reporter-options --reporter=nyan,delimeter=::

Coverage reports with nyc

nyc --reporter=lcov mocha -R @danmasta/mocha-sonar ./tests/unit/**/*.js

Use mocha and/or nyc as gulp tasks

const spawn = require('child_process').spawn;
const gulp = require('gulp');

gulp.task('test', () => {
    return spawn('mocha -R @danmasta/mocha-sonar ./tests/unit/**/*.js', {
        shell: true,
        stdio: ['inherit', 'inherit', 'inherit']
    });
});

gulp.task('coverage', () => {
    return spawn('nyc --reporter=lcov mocha -R @danmasta/mocha-sonar ./tests/unit/**/*.js', {
        shell: true,
        stdio: ['inherit', 'inherit', 'inherit']
    });
});

gulp.task('default', gulp.series('test', 'build'));
gulp.task('publish', gulp.series('coverage', 'build', 'deploy'));

Contact

If you have any questions feel free to get in touch

版本与发布记录

@danmasta/mocha-sonar 共发布 9 个版本,首次发布于 2019/03/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2024/01/122 年前
1.0.12021/03/185 年前
1.0.02020/08/026 年前
0.0.62019/05/227 年前
0.0.52019/04/237 年前
0.0.42019/03/167 年前

关于 @danmasta/mocha-sonar 的常见问题

@danmasta/mocha-sonar 是什么?

Mocha reporter for Sonarqube

如何安装 @danmasta/mocha-sonar?

在项目目录运行 npm install @danmasta/mocha-sonar,或使用 pnpm add / yarn add / bun add。

@danmasta/mocha-sonar 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/danmasta__mocha-sonar 是否存在。

@danmasta/mocha-sonar 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@danmasta/mocha-sonar 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@danmasta/mocha-sonar 可以商用吗?

@danmasta/mocha-sonar 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@danmasta/mocha-sonar 的下载量有多少?

根据 npm 官方下载统计,@danmasta/mocha-sonar 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,999 次,日均约 100 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@danmasta/mocha-sonar 还在维护吗?

最新版本 1.1.0 发布于 2024/01/12(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/03/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。