{"id":128897,"date":"2020-09-01T10:57:14","date_gmt":"2020-09-01T10:57:14","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=128897"},"modified":"2025-04-01T08:32:01","modified_gmt":"2025-04-01T08:32:01","slug":"jest-testing-tutorial","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/jest-testing-tutorial\/","title":{"rendered":"Jest Tutorial &#8211; JavaScript Unit Testing Using Jest Framework"},"content":{"rendered":"<p><strong>In this Jest tutorial, we will learn about various Jest features, Jest Matchers, and how to use the Jest framework for JavaScript Unit Testing:<\/strong><\/p>\n<p>Jest is a Javascript Testing framework built by Facebook.<\/p>\n<p>It is primarily designed for React (which is also built by Facebook) based apps but could be used to write automation scenarios for any Javascript-based codebases.<\/p>\n<p>In this Jest testing tutorial, we will learn about various features of Jest, its matchers and see how we can use Jest with an end to end example. We will also explore about code coverage using Jest.<\/p>\n<p><em><strong> <\/strong><\/em><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Jest-Introduction.png\"><img decoding=\"async\" class=\"alignnone wp-image-129091 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Jest-Introduction.png\" alt=\"Jest Tutorial\" width=\"700\" height=\"394\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Jest-Introduction.png 700w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Jest-Introduction-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><strong>List Of Tutorials In This JEST Series<\/strong><\/p>\n<p><span style=\"color: #ff6600;\"><strong>Tutorial #1:<\/strong><\/span> <a href=\"https:\/\/www.softwaretestinghelp.com\/jest-testing-tutorial\/\">Jest Tutorial &#8211; JavaScript Unit testing With Jest Framework<\/a><br \/>\n<span style=\"color: #ff6600;\"><strong>Tutorial #2:<\/strong><\/span> <a href=\"https:\/\/www.softwaretestinghelp.com\/testing-react-apps-using-jest\/\">How To Test React Apps Using Jest Framework<\/a><br \/>\n<span style=\"color: #ff6600;\"><strong>Tutorial #3:<\/strong><\/span> <a href=\"https:\/\/www.softwaretestinghelp.com\/jest-configuration-and-debugging\">Jest Configuration And Debugging Jest Based Tests<\/a><\/p>\n<hr \/>\n<h2>Getting Started With Jest Testing<\/h2>\n<p><strong>Some of the advantages\/features of Jest are given below:<\/strong><\/p>\n<ol>\n<li>Zero configuration required.<\/li>\n<li><strong>Fast:<\/strong> Jest tests run in parallel &#8211; this in turn greatly reduces the test execution time.<\/li>\n<li><strong>Built-in code coverage:<\/strong> Jest supports code coverage out of the box &#8211; this is a very useful metric for all CI-based delivery pipelines and overall test effectiveness of a project.<\/li>\n<li><strong>Isolated and sandboxed tests:<\/strong> Each Jest test runs in its own sandbox, which ensures no two tests can interfere or impact each other.<\/li>\n<li><strong>Powerful Mocking support:<\/strong> Jest tests support all types of mocking &#8211; be it functional mocking, timer mocking, or mocking individual API calls.<\/li>\n<li><strong>Support snapshot testing:<\/strong> Snapshot testing is relevant from the React perspective. Jest supports capturing a snapshot of the react component being tested &#8211; this can validate with the component\u2019s actual output. This greatly helps in validating the component\u2019s behavior.<\/li>\n<\/ol>\n<h2>Jest Framework For JavaScript Unit Testing<\/h2>\n<p>In this section, we will see an end to end example to write tests using the JEST framework for a simple Javascript function. Firstly, let&#8217;s see how to install the JEST framework in our project<\/p>\n<h3><span style=\"color: #ff6600;\">JEST Installation<\/span><\/h3>\n<p>Jest is simply a node package and can be installed using any node-based package manager. <span style=\"text-decoration: underline;\"><strong>Example,<\/strong><\/span> npm or yarn.<\/p>\n<p><strong>Let\u2019s see some sample commands that can be used to install Jest package.<\/strong><\/p>\n<pre>yarn add --dev jest<\/pre>\n<pre>npm install --save-dev jest<\/pre>\n<p>For installing the Jest module globally, you can simply use the \u2018-g\u2019 flag along with the npm command. This will enable you to use Jest commands directly without configuring the package file for npm tests.<\/p>\n<pre>npm install -g jest<\/pre>\n<h3><span style=\"color: #ff6600;\">Using Jest In A Node-based Project<\/span><\/h3>\n<p>To use Jest in a node-based project, simply use the commands from the above section to install the node package for Jest.<\/p>\n<p><strong>Follow the below steps, in order to create a node project from start and then install Jest into it.<\/strong><\/p>\n<p><strong>#1)<\/strong> Create a folder\/directory with a name as your project name, <span style=\"text-decoration: underline;\"><strong>for example<\/strong><\/span>, myFirstNodeProject<\/p>\n<p><strong>#2)<\/strong> Now using the terminal or command line, navigate to the project created in the above step and execute the npm init script using the below command.<\/p>\n<pre>npm init<\/pre>\n<p><strong>#3)<\/strong> Once the above command is executed, it will prompt for different questions\/parameters.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>For example<\/strong><\/span>, the name of the project, version, etc. Simply keep pressing enter (and accept the default values). Once completed, you will see a package.json file created in your project. This is a mandatory configuration file for any node-based project<\/p>\n<p><strong>#4)<\/strong> Now execute the command to install the Jest package into the newly created project using the below command.<\/p>\n<pre>npm install --save-dev jest<\/pre>\n<p>This will install the Jest module (as well as its dependencies).<\/p>\n<p><strong>#5)<\/strong> Now, we have a node project ready with Jest bindings. Let\u2019s configure the npm test script to run the Jest tests i.e. when the command \u2018npm test\u2019 is executed, the script should run all the Jest framework based tests.<\/p>\n<p>To do that, update the package.json file and add a script section as shown below.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&quot;scripts&quot;: {\r\n  &quot;test&quot;: &quot;jest&quot;\r\n }\r\n<\/pre>\n<p><strong>The final package.json file will look as shown below.<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n &quot;name&quot;: &quot;jest-e2e&quot;,\r\n &quot;version&quot;: &quot;1.0.0&quot;,\r\n &quot;description&quot;: &quot;&quot;,\r\n &quot;main&quot;: &quot;index.js&quot;,\r\n &quot;scripts&quot;: {\r\n   &quot;test&quot;: &quot;jest&quot;\r\n },\r\n &quot;author&quot;: &quot;&quot;,\r\n &quot;license&quot;: &quot;ISC&quot;,\r\n &quot;dependencies&quot;: {\r\n   &quot;jest&quot;: &quot;^25.1.0&quot;\r\n }\r\n}\r\n<\/pre>\n<h3><span style=\"color: #ff6600;\">Writing Tests For A Javascript Function<\/span><\/h3>\n<p>In this section, we will create a simple Javascript function code for addition, subtraction, and multiplication of 2 numbers and write the corresponding Jest based tests for it.<\/p>\n<p><strong>First, let\u2019s see how the code for our application (or function) under test looks like.<\/strong><\/p>\n<p><strong>#1)<\/strong> In the node project created in the above section, create a js file named calculator.js with contents as shown below<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst mathOperations = {\r\n   sum: function(a,b) {\r\n       return a + b;\r\n   },\r\n  \r\n   diff: function(a,b) {\r\n       return a - b;\r\n   },\r\n   product: function(a,b) {\r\n       return a * b\r\n   }\r\n}\r\n  module.exports = mathOperations\r\n<\/pre>\n<p><strong>#2)<\/strong> Now, create a test file in the same folder for these tests, named <strong>calculator.test.js<\/strong> &#8211; this is the convention expected by the Jest framework to look for all the files that contain Jest based tests. We will also import the function under test, in order to execute the code in the test.<\/p>\n<p>This is how the file would look with just import \/ require declaration.<\/p>\n<pre>const mathOperations = require('.\/calculator');<\/pre>\n<p><strong>#3)<\/strong> Now, let\u2019s write tests for different methods in the main file i.e. sum, diff, and product.<\/p>\n<p>Jest tests follow BDD style tests, with each test suite having one main describe block and can have multiple test blocks. Also, please note that the tests can have nested describe blocks as well.<\/p>\n<p>Let\u2019s write a test for adding 2 numbers and validate the expected results. We will be supplying the numbers as 1 &amp; 2 and expecting the output as 3.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndescribe(&quot;Calculator tests&quot;, () =&gt; {\r\n test('adding 1 + 2 should return 3', () =&gt; {\r\n   expect(mathOperations.sum(1, 2)).toBe(3);\r\n });\r\n})\r\n<\/pre>\n<p><strong>Please refer to the below points w.r.t the test above:<\/strong><\/p>\n<p><strong>a)<\/strong> The describe block is an outer description for the test suite i.e it represents a generic container for all the tests that we are going to write for the calculator in this file.<\/p>\n<p><strong>b)<\/strong> Next, we have an individual test block &#8211; this represents a single test. The string in quotes represents the name of the test.<\/p>\n<p><strong>c)<\/strong> Refer to the code in the expect block &#8211; \u201cexpect\u201d is nothing but an assertion. The statement is calling the sum method in the function under test with inputs 1 &amp; 2 and expecting the output to be 3.<\/p>\n<p>We can also rewrite this in a simpler way to understand it better.<\/p>\n<p>Please see below, now we have separated the function call and assertion as 2 separate statements to make it more succinct.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndescribe(&quot;Calculator tests&quot;, () =&gt; {\r\n test('adding 1 + 2 should return 3', () =&gt; {\r\n   \/\/ arrange and act\r\n   var result = mathOperations.sum(1,2)\r\n\r\n   \/\/ assert\r\n   expect(result).toBe(3);\r\n });\r\n})\r\n<\/pre>\n<p><strong>d)<\/strong> In order to run this test, simply run the command \u201c<strong>npm test<\/strong>\u201d in the terminal or command prompt at the project location.<\/p>\n<p><strong>You will see the output as shown below.<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Run-the-command-\u201cnpm-test\u201d-in-the-terminal.png\"><img decoding=\"async\" class=\"alignnone wp-image-129086 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Run-the-command-\u201cnpm-test\u201d-in-the-terminal.png\" alt=\"Run the command \u201cnpm test\u201d in the terminal - Output\" width=\"429\" height=\"201\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Run-the-command-\u201cnpm-test\u201d-in-the-terminal.png 429w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Run-the-command-\u201cnpm-test\u201d-in-the-terminal-300x141.png 300w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/a><\/p>\n<p><strong>#4)<\/strong> Let\u2019s try some more tests.<\/p>\n<p><strong>a)<\/strong> First, write a failing test and see what output we get. Let&#8217;s just change the result to some incorrect value in the same test that we wrote in the last section. See how the test looks like.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndescribe(&quot;Calculator tests&quot;, () =&gt; {\r\n test('adding 1 + 2 should return 10', () =&gt; {\r\n   \/\/ arrange and act\r\n   var result = mathOperations.sum(1,2)\r\n\r\n   \/\/ assert\r\n   expect(result).toBe(10);\r\n });\r\n})\r\n<\/pre>\n<p>Here we are expecting a sum of 1 and 2 to return 10 which is incorrect.<\/p>\n<p><strong>Let\u2019s try executing this and see what we get.<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/detailed-output-when-a-test-is-failed.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-129087\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/detailed-output-when-a-test-is-failed.png\" alt=\"detailed output when a test is failed\" width=\"495\" height=\"503\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/detailed-output-when-a-test-is-failed.png 495w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/detailed-output-when-a-test-is-failed-295x300.png 295w\" sizes=\"(max-width: 495px) 100vw, 495px\" \/><\/a><\/p>\n<p>You can see the detailed output when a test is failed i.e. what was actually returned and what was expected and which line caused the error in the function under test etc.<\/p>\n<p><strong>b)<\/strong> Let\u2019s write more tests for the other functions i.e difference and product.<\/p>\n<p>The test file with all the tests will look as shown below.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst mathOperations = require('.\/calculator');\r\n\r\ndescribe(&quot;Calculator tests&quot;, () =&gt; {\r\n test('adding 1 + 2 should return 3', () =&gt; {\r\n   \/\/ arrange and act\r\n   var result = mathOperations.sum(1,2)\r\n\r\n   \/\/ assert\r\n   expect(result).toBe(3);\r\n });\r\n\r\n test(&quot;subtracting 2 from 10 should return 8&quot;, () =&gt; {\r\n   \/\/ arrange and act\r\n   var result = mathOperations.diff(10,2)\r\n\r\n   \/\/ assert\r\n   expect(result).toBe(8);\r\n });\r\n\r\n test(&quot;multiplying 2 and 8 should return 16&quot;, () =&gt; {\r\n   \/\/ arrange and act\r\n   var result = mathOperations.product(2,8)\r\n\r\n   \/\/ assert\r\n   expect(result).toBe(16);\r\n });\r\n})\r\n<\/pre>\n<p>When the above tests are executed, <strong>the output given below gets generated.<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Tests-output-for-functions-difference-and-product.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-129088\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Tests-output-for-functions-difference-and-product.png\" alt=\"Tests output for functions - difference and product\" width=\"517\" height=\"270\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Tests-output-for-functions-difference-and-product.png 517w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Tests-output-for-functions-difference-and-product-300x157.png 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/a><\/p>\n<h3><span style=\"color: #ff6600;\">Video Tutorial: What Is Jest<\/span><\/h3>\n<p><iframe src=\"https:\/\/www.youtube.com\/embed\/Tcq64HEr_PY\" width=\"650\" height=\"350\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">?<\/span><\/iframe><\/p>\n<h2>Jest Matchers<\/h2>\n<p>Jest assertions use matchers to assert on a condition. Jest uses matchers from the expect Api. The expect API doc can be referenced <a href=\"https:\/\/jestjs.io\/docs\/en\/expect\" target=\"_blank\" rel=\"noopener nofollow\">here.<\/a><\/p>\n<p><strong>Let\u2019s walk through some of the most commonly used matchers along with Jest tests.<\/strong><\/p>\n<h3><span style=\"color: #ff6600;\">#1) Equality<\/span><\/h3>\n<p>These are the most commonly used matchers. They are used for checking equality or inequality and is mostly used for arithmetic operations.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Let\u2019s see some examples below:<\/strong><\/span><\/p>\n<p>Here we have written 2 matchers using toBe and not.toBe which are analogous to equals and not equals.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ntest(&quot;equality matchers&quot;, () =&gt; {\r\n   expect(2*2).toBe(4);\r\n   expect(4-2).not.toBe(1);\r\n })\r\n<\/pre>\n<h3><span style=\"color: #ff6600;\">#2) Truthiness<\/span><\/h3>\n<p>Here we will see, matchers for null, falsy, and truthy i.e. false and truth values. It&#8217;s important to note that anything that\u2019s not logically true is falsy.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>For example,<\/strong><\/span> number 0, null, empty string, NaN are all examples of falsy w.r.t Javascript.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ntest(&quot;truthy operators&quot;, () =&gt; {\r\n   var name=&quot;Software testing help&quot;\r\n   var n = null\r\n   expect(n).toBeNull()\r\n   expect(name).not.toBeNull\r\n\r\n   \/\/ name has a valid value\r\n   expect(name).toBeTruthy()\r\n\r\n   \/\/fail - as null is non success\r\n   expect(n).toBeTruthy()\r\n  \r\n   \/\/ pass - null treated as false or negative\r\n   expect(n).toBeFalsy()\r\n\r\n   \/\/ 0 - treated as false\r\n   expect(0).toBeFalsy()\r\n })\r\n<\/pre>\n<h3><span style=\"color: #ff6600;\">#3) Number Matchers<\/span><\/h3>\n<p>These matchers could be used for general arithmetic operations.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>For example,<\/strong><\/span> greaterThan, lessThan, greaterThanOrEqual, etc.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Check the below examples for more details<\/strong><\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ntest(&quot;numeric operators&quot;, () =&gt; {\r\n\r\n   var num1 = 100;\r\n   var num2 = -20;\r\n   var num3 = 0;\r\n\r\n   \/\/ greater than\r\n   expect(num1).toBeGreaterThan(10)\r\n\r\n   \/\/ less than or equal\r\n   expect(num2).toBeLessThanOrEqual(0)\r\n\r\n   \/\/ greater than or equal\r\n   expect(num3).toBeGreaterThanOrEqual(0)\r\n })\r\n<\/pre>\n<h3><span style=\"color: #ff6600;\">#4) String Matchers<\/span><\/h3>\n<p>A lot of times we need strings to match a regular expression as an assertion in a Unit test. Jest provides matchers for strings to be matched against a regular expression.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ntest(&quot;string matchers&quot;,() =&gt; {\r\n   var string1 = &quot;software testing help - a great resource for testers&quot;\r\n\r\n   \/\/ test for success match\r\n   expect(string1).toMatch(\/test\/);\r\n\r\n   \/\/ test for failure match\r\n   expect(string1).not.toMatch(\/abc\/)\r\n })\r\n<\/pre>\n<h3><span style=\"color: #ff6600;\">Video Tutorial: Jest Matchers<\/span><\/h3>\n<p><iframe src=\"https:\/\/www.youtube.com\/embed\/nibnVdaA9gM\" width=\"650\" height=\"350\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h2>Jest Hooks &#8211; Setup And Teardown<\/h2>\n<p>Just like all other xUnit based unit test frameworks, Jest framework also provides hooks for setup and cleanup methods. These hook methods are executed before and after each test in the test suite or before and after the testSuite execution.<\/p>\n<p><strong>Totally there are 4 hooks that are available to use.<\/strong><\/p>\n<ul>\n<li><strong>beforeEach and afterEach:<\/strong> These hooks are executed before and after each test in the test suite.<\/li>\n<li><strong>beforeAll and afterAll:<\/strong> These hooks are executed just once for each test suite. i.e. if a test suite has 10 tests, then these hooks will just be executed once for every test execution.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Let\u2019s see an example:<\/strong><\/span>\u00a0We will add these hooks to the same test example of adding 2 numbers.<\/p>\n<p><strong>We will set the inputs in beforeEach hook for illustration. The test file would look with test hooks as shown below.<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndescribe(&quot;Calculator tests&quot;, () =&gt; {\r\n  var input1 = 0\r\n var input2 = 0\r\n\r\n beforeAll(() =&gt; {\r\n   console.log(&quot;beforeAll called&quot;);\r\n });\r\n\r\n afterAll(() =&gt; {\r\n   console.log(&quot;afterAll called&quot;);\r\n });\r\n  beforeEach(() =&gt; {\r\n   console.log(&quot;beforeEach called&quot;);\r\n   input1 = 1;\r\n   input2 = 2;\r\n });\r\n  afterEach(() =&gt; {\r\n   console.log(&quot;afterEach called&quot;);\r\n });\r\n\r\n test('adding 1 + 2 should return 3', () =&gt; {\r\n   \/\/ arrange and act\r\n   var result = mathOperations.sum(input1,input2)\r\n\r\n   \/\/ assert\r\n   expect(result).toBe(3);\r\n });\r\n})\r\n<\/pre>\n<h3>Tips &amp; Tricks<\/h3>\n<p><strong>#1)<\/strong> The command line reports are good but not very readable. There are libraries\/modules available to generate HTML based test reports for Jest tests. It can be achieved as shown below.<\/p>\n<ul>\n<li>Add node package for jest-html-reporter using the below command.<\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">npm install --save-dev jest-html-reporter<\/span><\/pre>\n<ul>\n<li>Now add Jest configuration for the reporter in the package.json file of the node project.<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&quot;jest&quot;: {\r\n   &quot;reporters&quot;: &#x5B;\r\n     &quot;default&quot;,\r\n     &#x5B;\r\n       &quot;.\/node_modules\/jest-html-reporter&quot;,\r\n       {\r\n         &quot;pageTitle&quot;: &quot;Test Report&quot;\r\n       }\r\n     ]\r\n   ]\r\n }\r\n<\/pre>\n<ul>\n<li>Once configured now, execute the tests using the \u201c<strong>npm test<\/strong>\u201d command.<\/li>\n<li>If the setup is successful you should be able to see an Html based report getting created in the project directory.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Test-Report.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-129089\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Test-Report.png\" alt=\"Test Report\" width=\"542\" height=\"360\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Test-Report.png 542w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/Test-Report-300x199.png 300w\" sizes=\"(max-width: 542px) 100vw, 542px\" \/><\/a><\/p>\n<p><strong>#2) Creating Code coverage report: <\/strong>Code coverage is one of the most important metrics from a unit testing perspective. It essentially measures what percentage of statements\/branches are covered for the application under test.<\/p>\n<p>Jest provides out of the box support for code coverage. In order to get the Jest coverage report, <strong>Jest configuration<\/strong> needs to be added in the <strong><em>package.json<\/em><\/strong> file.<\/p>\n<p><strong>Add the configuration as shown below:<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&quot;jest&quot;: {\r\n     &quot;collectCoverage&quot;:true\r\n   }\r\n<\/pre>\n<p>Once this configuration is done, try running the tests using the command <strong>\u201cnpm test\u201d<\/strong>, and you can <strong>see the code coverage details just below the test execution results as shown below.<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/code-coverage-details.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-129090\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/code-coverage-details.png\" alt=\"code coverage details\" width=\"602\" height=\"285\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/code-coverage-details.png 602w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/05\/code-coverage-details-300x142.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><\/p>\n<h3><span style=\"color: #ff6600;\">Video Tutorial: Jest Coverage &amp; HTML Report Generation<\/span><\/h3>\n<p><iframe src=\"https:\/\/www.youtube.com\/embed\/dchwI3Rimxg\" width=\"650\" height=\"350\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">?<\/span><\/iframe><\/p>\n<h2>Conclusion<\/h2>\n<p>In this Jest tutorial, we walked through the basics of the Jest framework. We learned how to install the Jest framework, and saw how it can be used for testing simple Javascript files.<\/p>\n<p><strong>Also read =&gt;&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/javascript-frameworks\/\">Best Javascript Frameworks<\/a><\/strong><\/p>\n<p>We also explored the different types of matchers supported by Jest and covered Html reporters and code coverage reports too.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/testing-react-apps-using-jest\/\"><strong>NEXT Tutorial<\/strong><\/a><\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"128897\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>In this Jest tutorial, we will learn about various Jest features, Jest Matchers, and how to use the Jest framework for JavaScript Unit Testing: Jest is a Javascript Testing framework built by Facebook. It is primarily designed for React (which is also built by Facebook) based apps but could be &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Jest Tutorial &#8211; JavaScript Unit Testing Using Jest Framework\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/jest-testing-tutorial\/#more-128897\" aria-label=\"Read more about Jest Tutorial &#8211; JavaScript Unit Testing Using Jest Framework\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":129091,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[521],"tags":[],"class_list":{"0":"post-128897","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-javascript"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/128897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=128897"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/128897\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/129091"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=128897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=128897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=128897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}