This is a long standing question and searching for a solution leads to old or wrong answers, like that suggestion to add "disableOptimisticBPs: true", which is no longer supported. I couldn't even find a note saying that it was removed and why.
The main problem here is clearly the source map. For some reasons the generated source maps don't match the actual TS code. And for me not even breakpoints in the normal code worked (outside of test code). But in another project I have a working Mocha configuration, which finally brought me to a solution here.
You don't need ts-jest
for debugging! It's the culprit for the entire trouble, which caused not only for me to lose days, searching for a solution. Instead let tsc
generate your JS code (as you probably do anyway) and make it generating source maps too. Then just give Jest the path to the JS files, not the TS files. VS Code will automatically find the sidecar source maps and step with you through your TS code just fine.
As launch configuration to debug the current spec file use:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Run current Jest test",
"runtimeExecutable": null,
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand",
"${fileBasenameNoExtension}.js",
],
"console": "integratedTerminal",
"stopOnEntry": false,
"preLaunchTask": "tsc",
"outFiles": [
"${workspaceFolder}/output/src/**/*.js"
],
"env": {
"NODE_ENV": "testing"
}
}
]
}
This needs a task to run the TS compiler:
{
"version": "2.0.0",
"tasks": [
{
"command": "tsc",
"label": "tsc",
"type": "shell",
"args": [
"-w",
"-p",
"."
],
"isBackground": true,
"problemMatcher": "$tsc-watch",
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
]
}
which builds all file on first test debug run and then watches for any changes in the TS files and recompiles those changed immediately.
Now select any of your spec files, set a breakpoint (and maybe also in your source code) and start debugging.