Generating HTML Test Summary Reports with jest-html-reporter

Blake Bradford Avatar

·

Generating HTML Test Summary Reports with jest-html-reporter

As a software engineer or solution architect, it’s crucial to have a clear overview of your test results. Jest is a popular testing framework for JavaScript applications, and with the jest-html-reporter package, you can easily generate informative HTML test summary reports. In this article, we’ll explore how to install, configure, and use jest-html-reporter to generate comprehensive reports for your test results.

Installation

You can install jest-html-reporter using npm or yarn. Simply run the following command in your project directory:

bash
$ npm install jest-html-reporter --save-dev

or

bash
$ yarn add jest-html-reporter --dev

Usage

To configure Jest to process the test results and generate an HTML report, you need to add an entry to your Jest config file (jest.config.json). Here’s an example configuration:

json
"reporters": [
"default",
["./node_modules/jest-html-reporter", {
"pageTitle": "Test Report"
}]
]

With this configuration, running Jest from the terminal will generate a file called test-report.html in your project’s root directory. This file will contain all the relevant information about your tests.

Configuration Options

jest-html-reporter provides several configuration options to customize the report output. Here are some of the most commonly used options:

  • append – If set to true, new test results will be appended to the existing test report.
  • collapseSuitesByDefault – Determines whether test suites should be collapsed by default.
  • outputPath – The path where the HTML report will be generated. The default is "./test-report.html".
  • pageTitle – The title of the document and the top-level heading in the report.
  • logo – Path to a logo image that will be included in the header of the report.
  • sort – Sorts the test results using the given method. Available sorting methods can be found in the documentation.
  • styleOverridePath – The path to a file containing CSS styles that should override the default styling.

These are just a few examples of the options available. For a complete list of configuration options and their descriptions, please refer to the official documentation.

Compatibility with Node.js

jest-html-reporter is compatible with Node.js version ^4.8.3 and above. Make sure you have the required version of Node.js installed before using this package.

Using jest-html-reporter as a Test Results Processor

In addition to generating reports during test execution, you can also use jest-html-reporter as a test results processor. This means that the report will be generated after the Jest execution has completed. To configure jest-html-reporter as a test results processor, add the following entry to your Jest config file:

json
{
"testResultsProcessor": "./node_modules/jest-html-reporter"
}

Please note that when using jest-html-reporter as a test results processor, you need to create a separate configuration file named jesthtmlreporter.config.json in your project’s root directory. This file should contain the desired configuration options for the report.

Continuous Integration

If you’re using continuous integration for your project, you can configure jest-html-reporter using environment variables. This allows you to dynamically set file paths and report titles based on the current branch or environment. Here’s an example of dynamically naming the output file and setting the report title:

bash
export BRANCH_NAME=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3`
export JEST_HTML_REPORTER_OUTPUT_PATH=/home/username/jest-test-output/test-reports/"$BRANCH_NAME".html
export JEST_HTML_REPORTER_PAGE_TITLE="$BRANCH_NAME"\ Test\ Report

By setting these environment variables, you can have the report output file and title match the current branch or environment in an automated deployment pipeline.

Conclusion

With jest-html-reporter, you can easily generate HTML test summary reports for your Jest test results. This package provides various configuration options to customize the report output according to your needs. You can also use it as a test results processor or configure it with environment variables for seamless integration into your continuous integration workflow. Give jest-html-reporter a try and enhance your testing process with informative and visually appealing reports.

For more information and detailed documentation, please visit the official jest-html-reporter repository.

Happy testing!

References

Leave a Reply

Your email address will not be published. Required fields are marked *