What is jest-junit?
The jest-junit npm package is a Jest reporter that creates compatible JUnit XML reports. It's primarily used in CI/CD pipelines to integrate test results with tools that consume JUnit XML format, such as Jenkins, CircleCI, and others. This allows for better visualization of test outcomes and more efficient debugging when tests fail.
What are jest-junit's main functionalities?
Generating JUnit XML reports
This command runs Jest tests and generates a JUnit XML report. The '--reporters' option specifies that both the default reporter and jest-junit should be used, allowing for output in the console as well as the creation of an XML report.
jest --reporters=default --reporters=jest-junit
Customizing output location and file name
This example demonstrates how to customize the output directory and file name of the JUnit XML report using environment variables. The report will be saved to './artifacts/my_test_results.xml'.
JEST_JUNIT_OUTPUT_DIR='./artifacts' JEST_JUNIT_OUTPUT_NAME='my_test_results.xml' jest --reporters=default --reporters=jest-junit
Configuring through jest.config.js
This code snippet shows how to configure jest-junit through Jest's configuration file, jest.config.js. It specifies custom values for the suite name, output directory, and file name of the report.
module.exports = { reporters: ['default', ['jest-junit', { suiteName: 'My Suite', outputDirectory: './reports', outputName: 'results.xml' }]] };
Other packages similar to jest-junit
mocha-junit-reporter
Similar to jest-junit, mocha-junit-reporter is designed for Mocha tests. It generates JUnit XML reports for Mocha test suites. While jest-junit is tailored for Jest, mocha-junit-reporter offers similar functionality for projects using Mocha as their testing framework.
karma-junit-reporter
karma-junit-reporter is a plugin for the Karma test runner that outputs test results in JUnit XML format. It serves a similar purpose for Karma-based projects, providing integration with CI/CD tools that require JUnit XML reports. Compared to jest-junit, it's specific to the Karma ecosystem.
jasmine-reporters
jasmine-reporters is a package that provides JUnit XML reporting for Jasmine, another popular testing framework. It includes a JUnit reporter among other reporter types. This package is analogous to jest-junit for projects that use Jasmine, enabling them to generate JUnit-compatible reports.
jest-junit
A Jest reporter that creates compatible junit xml files
Installation
yarn add --dev jest-junit
Usage
In your jest config add the following entry:
{
"testResultsProcessor": "./node_modules/jest-junit"
}
Then simply run:
jest
Configuration
jest-junit
offers five configurations based on environment variables or a jest-junit
key defined in package.json
. All configuration values should be strings.
Variable Name | Description | Default |
---|
JEST_SUITE_NAME | name attribute of <testsuites> | "jest tests" |
JEST_JUNIT_OUTPUT | File path to save the output. | "./junit.xml" |
JEST_JUNIT_CLASSNAME | Template string for the classname attribute of <testcase> . | "{classname} {title}" |
JEST_JUNIT_TITLE | Template string for the name attribute of <testcase> . | "{classname} {title}" |
JEST_JUNIT_ANCESTOR_SEPARATOR | Character(s) used to join the describe blocks. | " " |
JEST_USE_PATH_FOR_SUITE_NAME | Use file path as the name attribute of <testsuite> | "false" |
Example:
JEST_SUITE_NAME="Jest JUnit Unit Tests" JEST_JUNIT_OUTPUT="./artifacts/junit.xml" jest
You can also define a jest-junit
key in your package.json
. All are string values.
{
...
"jest-junit": {
"suiteName": "jest tests",
"output": "./junit.xml",
"classNameTemplate": "{classname}-{title}",
"titleTemplate": "{classname}-{title}",
"ancestorSeparator": " › ",
"usePathForSuiteName": "true"
}
}
For the following test:
describe('addition', () => {
describe('positive numbers', () => {
it('should add up', () => {
expect(1 + 2).toBe(3);
});
});
});
The default output:
<testsuites name="jest tests">
<testsuite name="addition" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:42:28" time="0.161">
<testcase classname="addition positive numbers should add up" name="addition positive numbers should add up" time="0.004">
</testcase>
</testsuite>
</testsuites>
Changing the classNameTemplate
and titleTemplate
:
JEST_JUNIT_CLASSNAME="{classname}" JEST_JUNIT_TITLE="{title}" jest
<testsuites name="jest tests">
<testsuite name="addition" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:45:42" time="0.154">
<testcase classname="addition positive numbers" name="should add up" time="0.005">
</testcase>
</testsuite>
</testsuites>
Changing just the ancestorSeparator
:
JEST_JUNIT_ANCESTOR_SEPARATOR=" › " jest
<testsuites name="jest tests">
<testsuite name="addition" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:47:12" time="0.162">
<testcase classname="addition › positive numbers should add up" name="addition › positive numbers should add up" time="0.004">
</testcase>
</testsuite>
</testsuites>