Skip to main content

Jest

JUnit XML serves as a standard format for integration between tools that deal with test results. BuildPulse reads test results using this format, and you can use it for your analysis as well.

In the steps below, we'll start with a JavaScript project that has an existing Jest test suite, and we'll add JUnit XML as an additional output format for the test suite. In each step, we'll show the Git diff for the change that we're making.

1. Add jest-junit as a development dependency.

npm install --save-dev jest-junit

Verify that your package.json file includes this new dependency:

@@ -13,6 +13,7 @@
"author": "Richard Hendricks",
"license": "MIT",
"devDependencies": {
- "jest": "^26.6.3"
+ "jest": "^26.6.3",
+ "jest-junit": "^12.0.0"
}

2. Update your jest.config.js file to enable the jest-junit reporter in addition to the default reporter as shown below.

The diff below also configures various improvements to the JUnit XML output, like including the file path for each test.

@@ -4,5 +4,13 @@
*/

module.exports = {
- reporters: [ "default" ]
+ "reporters": [
+ "default",
+ ["jest-junit", {
+ addFileAttribute: "true",
+ ancestorSeparator: " › ",
+ classNameTemplate: "{classname}",
+ titleTemplate: "{title}",
+ }]
+ ]
};

3. By default, jest-junit writes the report to a file named junit.xml at the root of your project.

Add this file to your .gitignore file so that it doesn't accidentally get checked into the repository.

@@ -1 +1,2 @@
+/junit.xml
/node_modules
Commit these changes to your repository.

4. Commit these changes to your repository.

git commit -am "Update CI to generate JUnit XML for test results"

The final result of these changes should resemble commit 674249d in the buildpulse-example-jest repository.