Skip to main content

Jest

Jest is a JavaScript testing framework focused on simplicity. For installing it in a Node.js project and the common CLI flags, see Node.js.

Debug

Native node inspect + Chrome DevTools

IDE independent method.

Requires first launching the command to run the test in an "inspect mode" and then open the DevTools to perform the code debugging.

node --inspect --inspect-brk node_modules/.bin/jest test_file_to_run.js

Open Chrome and go to the URL "chrome://inspect/#devices".

To start debugging, press "inspect" and the inspector screen will be shown.

In case we don’t see our code, we just have to press "+ Add to workspace" and select the folder with our code.

Now we can use the debugger options(add breakpoints, go next, step over, etc…) to debug the code.

Close the inspector manually to exit debugging.

VSCode script runner

Replace Chrome DevTools inspector and use VSCode to debug our code directly in our code editor.

First task is to select the Debug menu in the sidebar.

Then we click on "Run and Debug".

Choose Node.js. This will create a file in our directory “.vscode/launch.json where our configurations will be stored.

Now we are going to add our “attach” configuration to debug our test files. For that, in the same debug view we are going to select "Add Configuration".

On the list we select "Node.js: Attach". The launch.json generated.

By now we are not going to need the other configuration, which is created when the file was created. So our file ends like:

{
"version": "0.2.0",
"configurations": [
{
"name": "Attach",
"port": 9229,
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
}
]
}

Now we are ready to start debugging our files. As we have done before, we run the command to start debugging:

node --inspect --inspect-brk node_modules/.bin/jest test_file_to_run.js

and then attach the process to debugger in VSCode.

Now we can just add breakpoint to debug our process and in the same way we have done with Chrome inspector.

VSCode extension

The easiest way to debug (and run) test case and moreover a single test file is using one of the available extensions for our VSCode.

For this example we are going to use the extension "Jest Runner".

Once installed, we are going to have a context menu to run or debug our test case. We just have to right-click over a test file and we can choose the action we want.

Jest Runner context menu in VS Code

The only thing you need to do is to add the desired breakpoint before select "Debug Jest" and the debugging process will start inside your VSCode with all the previously seen options to debug our code.

Async tests and timeouts

Example:

describe('Test', () => {
let data;
beforeAll(async () => {
data = await doAsyncTask();
});
afterAll(() => {
clean();
});

it('check data', () => {
expect(data).toEqual(true);
});
});

While debugging you may see the code in afterAll run before the code in beforeAll finishes. This happens when the test timeout is shorter than the time spent stepping through the code in the debugger. Increase the Jest timeout to fix it:

  • In code: jest.setTimeout(50000);
  • On the command line: --testTimeout=NUMBER