Table of contents
1.
Introduction
2.
Debugging Failed Tests
2.1.
Program
2.2.
Test
2.3.
Output
3.
Debugger
3.1.
Setting up Debugger in VS Code.
3.2.
Sample Debugging
3.2.1.
Adding Breakpoints
3.2.2.
Running
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Debugging in Jest

Author Aditya Anand
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! In this blog, we will learn how to handle failed test cases. We will also look at the step-by-step process of setting up a debugger for Jest in VS Code and perform a sample debugging to see how it actually works.

VS Code is a code editor which comes with an inbuilt Terminal, Source Control (Git), smart completions with Intellisense, etc. VS Code is highly customizable and fast. Please go through this post if you don’t have VS Code already installed in your machine.

 Let's get started. I hope you learn a lot from this article.

Debugging Failed Tests

While testing, we will eventually encounter a failed test case. Jest provides detailed information like expected and received values in matcher, line where test fails, etc.

Let’s make an example function and write a failed test for it, to understand better.

Program

// creating functions
const functions = {
function: (param) => {
     return param;
}
};
//export the function
module.exports = functions

The function is simply returning the parameter.

Test

// importing functions
const functions = require('./function.js');
// test
test('failed test', () => {
expect(functions.function(5)).toBe(4);
});

In the test, we have called function with 5, which will return 5, and in the matcher, we expect it to match with 4, which is why the test will fail.

Run the test using the command npm test.

Output

As we can see, Jest specifies the line where the test fails, and it also provides what is expected in the matcher and what is received.

Debugger

Debugging allows testers to stop and verify the code at specific breakpoints; this is very important because, in order to make large and complex codes bug-free, we need a proper methodology to tackle with

Setting up Debugger in VS Code.

Click on Run and Debug in the sidebar in VS code, or simply press ctrl + shift + D.

Go to the run configuration and click on add configuration.

A launch.json file will be opened, modify the launch.json file with the following code and save.

{
    "version": "0.2.0",
    "configurations": [
    {
        "name": "Debug Jest Tests",
        "type": "node",
        "request": "launch",
        "runtimeArgs": [
        "--inspect-brk",
        "${workspaceRoot}/node_modules/jest/bin/jest.js",
        "--runInBand"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen",
        "port": 9229
    }
    ]
  }

Now our debugging setup is ready, let’s try debugging some code.

Sample Debugging

In order to debug, we need to add breakpoints in our code where we want our debugger to stop in each test.

Adding Breakpoints

We can add our breakpoints in function as well as test.

Adding breakpoint is very easy in VS code. Hover over the left of the line and click, a red dot will appear, and a breakpoint is added, as shown below.

 

Running

Simply click the Start debugging button or press F5.

Jest will provide a lot of information like variables, watch, call stack, loaded scripts, breakpoints, to help testers in debugging process.

As shown below, our code should stop at each breakpoint after running the debugger.

Use the below buttons to continue, step over, step into, step out, restart and stop the debugger.

 After we go through all breakpoints test will execute and finish.

FAQs

  1. What is simulate jest?
    simulate() on the actual node that has the event handler set. simulate() will target the component's prop based on the event you give it.
     
  2. Does Jest have cache?
    The default cache directory can be found by calling jest --showConfig . Note: clearing the cache will reduce performance.
     
  3. How do I speed up Jest test?
    When you have Jest as your test runner, passing the --maxWorkers=50% option will make the tests faster in most cases. For watch mode, use --maxWorkers=25%, and for CI disable Jest workers with --runInBand.
     
  4. Do Jest tests run sequentially?
    Once the described blocks are complete, by default, Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.

Key Takeaways

For a failed test case, Jest specifies in which line test fails. It also shows the received value and value is expected. For detailed debugging, we can set up a debugger in VS Code; we have seen the step-by-step process to set up a debugger, information like variables, call stack, breakpoints, etc., provided by debugger help testers to test and debug any complex code quickly.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass