Debug Jest with VSCode
Learn how to debug your Jest tests with VSCode using Nx and the Angular CLI
2 min read
2 min read
Jest is quite established in the React community. In contrast, the Angular community is mostly leaning towards the Karma + Jasmine setup when it comes to the unit test runner. That’s mostly because that’s what is generated when creating a new project with the Angular CLI. But Jest is definitely not limited to React. In fact a lot of Angular devs start to pick it up, to the point where we at Nrwl (yes I joined a couple of months ago) we decided to use it as the default when generating a new project in Nx (regardless of Angular or React).
Debugging unit tests written with Karma is quite straightforward. Make sure you use some Browser as the runner (i.e. Chrome). Start the test in watch mode and then click the “Debug” button on the upper right corner. That’ll open up the test run in debug mode which will allow you to open your browser devtools and debug just as you’d normally do with your webapp.
I mentioned this previously when troubleshooting some weird error in a Karma & Jasmine based test
With Jest it is a bit different. Jest uses JSDom underneath, so there is no real browser running. Jest uses Node. However, you can create a handy VSCode launch configuration to allow you to debug your tests from within your IDE.
Here’s a short video I recorded to walk you through.
(Also, console.log
works surprisingly well many times π)
Note, this also works with a plain normal Angular CLI project π.
Here’s the snippet for easy copy & paste
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/@angular/cli/bin/ng",
"cwd": "${workspaceFolder}",
"args": [
"test",
"--testPathPattern=${fileBasenameNoExtension}",
"--runInBand"
],
"console": "internalConsole",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File (using nx)",
"program": "${workspaceFolder}/node_modules/@nrwl/cli/bin/nx.js",
"cwd": "${workspaceFolder}",
"args": [
"test",
"--testPathPattern=${fileBasenameNoExtension}",
"--runInBand"
],
"console": "internalConsole",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
}
]
}