@casualbot/jest-sonar-reporter

中文文档
2.7.1发布于 2026/05/13MITTypeScriptCommonJS构建来源已验证近 30 天下载 8.8万

A Sonar test reporter for Jest.

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Usage as testResultsProcessor (deprecated)
  4. Configuration

Jest Sonar Reporter

Quality Gate Status Maintainability Rating

SonarQube Cloud

@casualbot/jest-sonar-reporter is a custom results processor for Jest derived from Christian W. original work here.

It has been updated to be usable as a reporter in the jest.config, as well as, provide the ability to output relative paths for the generated XML file.

Installation

Using npm:

npm install --save-dev @casualbot/jest-sonar-reporter

Using yarn:

yarn add --dev @casualbot/jest-sonar-reporter

Usage

In your jest config add the following entry:

{
  "reporters": [ "default", "@casualbot/jest-sonar-reporter" ]
}

Then simply run:

jest

For your Continuous Integration you can simply do:

jest --ci --reporters=default --reporters=@casualbot/jest-sonar-reporter

Usage as testResultsProcessor (deprecated)

The support for testResultsProcessor is only kept for [legacy reasons][test-results-processor] and might be removed in the future. You should therefore prefer to configure @casualbot/jest-sonar-reporter as a reporter.

Should you still want to, add the following entry to your jest config:

{
  "testResultsProcessor": "@casualbot/jest-sonar-reporter"
}

Then simply run:

jest

For your Continuous Integration you can simply do:

jest --ci --testResultsProcessor="@casualbot/jest-sonar-reporter"

Configuration

@casualbot/jest-sonar-reporter offers several configurations based on environment variables or a @casualbot/jest-sonar-reporter key defined in package.json or a reporter option. Environment variable and package.json configuration should be strings. Reporter options should also be strings exception for suiteNameTemplate, classNameTemplate, titleNameTemplate that can also accept a function returning a string.

Environment Variable NameReporter Config NameDescriptionDefaultPossible Injection Values
JEST_SUITE_NAMEsuiteNamename attribute of <testsuites>"jest tests"N/A
JEST_SONAR_OUTPUT_DIRoutputDirectoryDirectory to save the output.process.cwd()N/A
JEST_SONAR_OUTPUT_NAMEoutputNameFile name for the output."jest-report.xml"N/A
JEST_SONAR_OUTPUT_FILEoutputFileFullpath for the output. If defined, outputDirectory and outputName will be overriddenundefinedN/A
JEST_SONAR_56_FORMATformatForSonar56Will generate the xml report for Sonar 5.6falseN/A
JEST_SONAR_RELATIVE_PATHSrelativePathsWill use relative paths when generating the xml reportfalseN/A
JEST_SONAR_UNIQUE_OUTPUT_NAMEuniqueOutputNameCreate unique file name for the output jest-sonar-report-${uuid}.xml, overrides outputNamefalseN/A
JEST_SONAR_SUITE_NAMEsuiteNameTemplateTemplate string for name attribute of the <testsuite>."{title}"{title}, {filepath}, {filename}, {displayName}
JEST_SONAR_CLASSNAMEclassNameTemplateTemplate string for the classname attribute of <testcase>."{classname} {title}"{classname}, {title}, {suitename}, {filepath}, {filename}, {displayName}
JEST_SONAR_TITLEtitleTemplateTemplate string for the name attribute of <testcase>."{classname} {title}"{classname}, {title}, {filepath}, {filename}, {displayName}
JEST_SONAR_ANCESTOR_SEPARATORancestorSeparatorCharacter(s) used to join the describe blocks." "N/A
JEST_SONAR_ADD_FILE_ATTRIBUTEaddFileAttributeAdd file attribute to the output. This config is primarily for Circle CI. This setting provides richer details but may break on other CI platforms. Must be a string."false"N/A
JEST_SONAR_INCLUDE_CONSOLE_OUTPUTincludeConsoleOutputAdds console output to any testSuite that generates stdout during a test run.falseN/A
JEST_SONAR_INCLUDE_SHORT_CONSOLE_OUTPUTincludeShortConsoleOutputAdds short console output (only message value) to any testSuite that generates stdout during a test run.falseN/A
JEST_SONAR_REPORT_TEST_SUITE_ERRORSreportTestSuiteErrorsReports test suites that failed to execute altogether as error. Note: since the suite name cannot be determined from files that fail to load, it will default to file path.falseN/A
JEST_SONAR_NO_STACK_TRACEnoStackTraceOmit stack traces from test failure reports, similar to jest --noStackTracefalseN/A
JEST_USE_PATH_FOR_SUITE_NAMEusePathForSuiteNameDEPRECATED. Use suiteNameTemplate instead. Use file path as the name attribute of <testsuite>"false"N/A

You can configure these options via the command line as seen below:

JEST_SUITE_NAME="Jest JUnit Unit Tests" JEST_SONAR_OUTPUT_DIR="./artifacts" jest

Or you can also define a @casualbot/jest-sonar-reporter key in your package.json. All are string values.

{
  ...,
  "jest": {
    "rootDir": ".",
    "testResultsProcessor": "@casualbot/jest-sonar-reporter"
  },
  "@casualbot/jest-sonar-reporter": {
    "suiteName": "jest tests",
    "outputDirectory": "coverage",
    "outputName": "jest-report.xml",
    "uniqueOutputName": "false",
    "classNameTemplate": "{classname}-{title}",
    "titleTemplate": "{classname}-{title}",
    "ancestorSeparator": " › ",
    "usePathForSuiteName": "true",
    "relativePaths": "true"
  }
}

Or you can define your options in your reporter configuration.

// jest.config.js
{
    reporters: [
        'default', 
        [ 
            '@casualbot/jest-sonar-reporter',
            {
                relativePaths: true,
                outputName: 'sonar-report.xml',
                outputDirectory: 'coverage'
            }
        ]
    ],
}

版本与发布记录

@casualbot/jest-sonar-reporter 共发布 19 个版本,首次发布于 2021/12/10;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.7.1latest2026/05/135 个月前
2.7.02026/04/286 个月前
2.6.02026/04/146 个月前
2.5.12026/04/057 个月前
2.5.02025/12/0611 个月前
2.4.02024/09/082 年前

关于 @casualbot/jest-sonar-reporter 的常见问题

@casualbot/jest-sonar-reporter 是什么?

A Sonar test reporter for Jest.

如何安装 @casualbot/jest-sonar-reporter?

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

@casualbot/jest-sonar-reporter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@casualbot/jest-sonar-reporter 支持 ESM 还是 CommonJS?

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

@casualbot/jest-sonar-reporter 需要什么版本的 Node.js?

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

@casualbot/jest-sonar-reporter 可以商用吗?

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

@casualbot/jest-sonar-reporter 的下载量有多少?

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

@casualbot/jest-sonar-reporter 还在维护吗?

最新版本 2.7.1 发布于 2026/05/13(5 个月前);至今共发布 19 个版本,其中过去 12 个月发布了 5 个;首次发布于 2021/12/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。