Table of contents
1.
Introduction
2.
Configuring Jest in Node
3.
HTML Report Generation
4.
FAQS
5.
Key Takeaways
Last Updated: Mar 27, 2024

Jest HTML Report Generation

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Jest is a framework in Javascript that is used for the testing purposes of large web applications. It is maintained by Facebook. Since Jest doesn’t require a lot of configuration, it is an excellent option to be used as a testing framework among new developers. It uses a variety of languages to work with projects, like TypeScript, Vue.js, Node, Babel, and Svelte. The main focus of Jest is to provide simplicity and support for large web applications.

Configuring Jest in Node

Jest can be easily configured with Node. Let’s see how to implement Jest in a very basic Node application.

Firstly we need to use the JavaScript package manager “npm” to install the Jest package in Node.

npm install --save-dev jest

 Let us consider we have a web application to calculate the product of two numbers,

function prod(x, y)
{
return a*b;
}
module.exports = prod;

We save the above code as prod.js, and the test case file will be named prod.test.js.

const prod = require(“./sum”)
test(“prod of 2 and 4 equals 8, () => {
except(prod(2,4).toBe(8);
});

Then we can efficiently perform the Jest Testing by executing the following code on the command prompt or terminal.

npm run test

HTML Report Generation

We can conveniently fetch the test reports in an HTML format with the help of the “jest-html-reporter” package present in Node. First, we will install the package using npm.

npm install --save-dev jest-html-report

We will have to add another file in the exact location as that of package.json. We will name the file “jesthtmlreporter.config.json.” We can add various options in this file in JSON format, but we will add the following configurations to the file for now.

{
 "pageTitle": "HTML Report",
 "outputPath": "report.html",
 "includeFailureMsg": true
}

And as the last step of our configuration process, we will add the following set of instructions to our package.json file under the script section.

"test:CI": "set CI=true &&react-scripts test --env=jsdom --testResultProcessor =./node_modules/jest-html-reporter"

“set CI=true” ensures that the tests end as soon as all the tests are successfully executed.

“--env=jsdom” provides the tests with a mock browser environment.

Now that we are done with all the configurations, all we have to do is run the “test:CI” command,

npm run test:CI

As we have provided the output path as report.html, the HTML report will be stored in the root directory of the application, named report.html.

We can further fetch the stats of our test coverage using the below commands,

npm run test -- --coverage

FAQS

  1. What is Jest?
    Jest is a framework in JavaScript that is used for the testing purposes of large web applications. It is maintained by Facebook. Since Jest doesn’t require a lot of configuration, it is an excellent option to be used as a testing framework among new developers.
     
  2. What is jest-html-reporter?
    jest-html-reporter is a package in Node used to generate an HTML test report. It can be easily incorporated into the web application using the Javascript package manager and making a few configuration changes.
     
  3. Can we use Jest for any other environment other than Node?
    Jest is specifically designed to be used with JavaScript and React, so any environment using JavaScript or React can be configured with Jest.
     
  4. What is the disadvantage of using Jest?
    Although Jest is a compelling library, it is comparatively prolonged compared to other testing libraries.
     
  5. How can we fetch the coverage of our test report?
    We can fetch the coverage of the test using the below mentioned line of code in Node.
npm run test -- --coverage

Key Takeaways

This Blog covered all the necessary points about Jest and the essential steps to generate a Jest report in HTML format.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass