Package detail

@casualbot/jest-sonar-reporter

CasualBot133.7kMIT2.4.0

A Sonar test reporter for Jest.

sonar, sonarqube, jest, reporter

readme

Jest Sonar Reporter

Quality Gate Status Maintainability Rating

@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 Name Reporter Config Name Description Default Possible Injection Values
JEST_SUITE_NAME suiteName name attribute of <testsuites> "jest tests" N/A
JEST_SONAR_OUTPUT_DIR outputDirectory Directory to save the output. process.cwd() N/A
JEST_SONAR_OUTPUT_NAME outputName File name for the output. "jest-report.xml" N/A
JEST_SONAR_OUTPUT_FILE outputFile Fullpath for the output. If defined, outputDirectory and outputName will be overridden undefined N/A
JEST_SONAR_56_FORMAT formatForSonar56 Will generate the xml report for Sonar 5.6 false N/A
JEST_SONAR_RELATIVE_PATHS relativePaths Will use relative paths when generating the xml report false N/A
JEST_SONAR_UNIQUE_OUTPUT_NAME uniqueOutputName Create unique file name for the output jest-sonar-report-${uuid}.xml, overrides outputName false N/A
JEST_SONAR_SUITE_NAME suiteNameTemplate Template string for name attribute of the <testsuite>. "{title}" {title}, {filepath}, {filename}, {displayName}
JEST_SONAR_CLASSNAME classNameTemplate Template string for the classname attribute of <testcase>. "{classname} {title}" {classname}, {title}, {suitename}, {filepath}, {filename}, {displayName}
JEST_SONAR_TITLE titleTemplate Template string for the name attribute of <testcase>. "{classname} {title}" {classname}, {title}, {filepath}, {filename}, {displayName}
JEST_SONAR_ANCESTOR_SEPARATOR ancestorSeparator Character(s) used to join the describe blocks. " " N/A
JEST_SONAR_ADD_FILE_ATTRIBUTE addFileAttribute Add 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_OUTPUT includeConsoleOutput Adds console output to any testSuite that generates stdout during a test run. false N/A
JEST_SONAR_INCLUDE_SHORT_CONSOLE_OUTPUT includeShortConsoleOutput Adds short console output (only message value) to any testSuite that generates stdout during a test run. false N/A
JEST_SONAR_REPORT_TEST_SUITE_ERRORS reportTestSuiteErrors Reports 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. false N/A
JEST_SONAR_NO_STACK_TRACE noStackTrace Omit stack traces from test failure reports, similar to jest --noStackTrace false N/A
JEST_USE_PATH_FOR_SUITE_NAME usePathForSuiteName DEPRECATED. 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'
            }
        ]
    ],
}

changelog

2.4.0

  • Fix package name in README (scanner -> reporter) #23
  • Add files to solve NPM 10 lib/src strip problem #22
  • Add projectRoot option for relativePaths #20

2.2.5

  • fix: update documentation in README

2.2.4

  • chore: update CHANGELOG
  • fix: add .scarnnerwork to npmignore and add CHANGELOG

2.2.3

  • fix: update readme
  • set proper version
  • Update README.md
  • Update LICENSE
  • Update README.md
  • update version
  • Update README (#4)
  • update readme (#3)
  • Update to create jest-sonar-reporter (#1)
  • Merge branch 'master' into develop
  • Merge develop to release version v2.0.0
  • Chore: Add templates for GitHub.
  • Chore: Update keywords for NPM.
  • Documentation: Update LICENSE
  • Merge branch 'jest-sonar-reporter-11' into develop
  • Documentation: Support different configuration environments.
  • Refactoring: Reduce cyclomatic complexity.
  • Feature: Support different configuration environments.
  • Refactoring: Export default configuration.
  • Refactoring: Reorganise project structure.
  • Chore: Ignore test report.
  • Refactoring: Reduce cyclomatic complexity.
  • Fix: Fetch quality status from SonarCloud.
  • Fix: Coverage Broken: metrics.isEmpty is not a function at tableRow
  • Chore: Update minimum Node version to 8 LTS.
  • Merge branch 'jest-sonar-reporter-14' into develop
  • Fix: Fetch quality status from SonarCloud.
  • Feature: Drop support for env-cmd.
  • Chore: Upgrade dev dependencies.
  • Merge branch 'master' into develop
  • Merge develop to release version v1.3.0
  • Ensure Node4 compatibility.
  • Merge pull request #12 from Dubes/support_for_sonar_5_6
  • jest-sonar-reporter-13 Sonarqube addon has been renamed to Sonarcloud.
  • Undo changes done by prettier
  • Add support for Sonarqube 5.6.x
  • Merge branch 'master' into develop
  • Merge branch 'develop'
  • v1.2.0-4
  • Travis needs organization key for SonarQube.
  • Update token for SonarQube.
  • Deprecate old configuration option.
  • Fixes typo.
  • jest-sonar-reporter-9 Adds documentation for the new configuration options.
  • Configure scanned branches.
  • Adds script to update Sonar's project version.
  • Upgrades version of Jest.
  • jest-sonar-reporter-8 Adds support to use package.json for configuration.
  • Refactoring
  • Cleans up generated reports.
  • Merge pull request #7 from qtell/master
  • testResultsProcessor function is now required to return the modified results
  • v1.1.0
  • jest-sonar-reporter-5 Add support for stack traces.
  • Enable debug output.
  • jest-sonar-reporter-4 Replace TestExecutions class with function.
  • jest-sonar-reporter-3 Replace File class with function.
  • jest-sonar-reporter-2 Replace TestCase class with function.
  • jest-sonar-reporter-1 Replace Failure class with function.
  • Run tests with coverage.
  • Provide proper documentation.
  • Provide information for publishing.
  • Travis CI integration.
  • Import test results and coverage information into Sonar.
  • Write test results into XML file.
  • Use 'fullName' and 'duration' for test case.
  • Test full report.
  • Adds failure tag with message information.
  • Adds test case tag with name and duration information.
  • Adds file tag with path information.
  • Adds root tag with version information.
  • Project setup.
  • Initial commit