I've recently switched from Vim to VSCode, and I'm trying to set up VSCode debugging for jest tests run through docker.
The debugging works...sort of. If I want to run jest tests and have breakpoints activate, I need to:
vscode-jest-tests
launch.json task belowDocker: Attach To Node
before the test suite hits breakpointsObviously not ideal - I'd love to ensure VSCode automatically attaches to the debugger upon running vscode-jest-tests
. In short: is there an easy way to attach the VSCode debugger when running Jest tests through Docker?
Here are my current launch.json and package.json files. Any help very appreciated:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/www",
"protocol": "inspector"
},
{
"type": "node",
"request": "launch",
"name": "vscode-jest-tests",
"runtimeExecutable": "npm",
"runtimeArgs": [ "run", "test:debug" ],
"address": "127.0.0.1",
"port": 9229,
"breakOnLoad": true,
"restart": true,
"timeout": 10000,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/www",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
#...
"scripts": {
"test:debug": "docker exec -it kiva_api node --nolazy --inspect-brk=0.0.0.0:9229 node_modules/.bin/jest --runInBand --config=test/jest-e2e.json"
}
#...
PS: If I run npm run test:debug
from the command line & have a chrome debugger window open, Chrome's debugger works just fine
Here a shot of my solution, which comes mostly from your same question. Thank you for asking :)
First start jest
in watch mode (--watchAll
), so the process stays alive. (In the snippet I assume a backend
container is running via docker-compose
with port 9229
exposed on the host)
docker-compose exec backend \
node --inspect=0.0.0.0:9229 -r tsconfig-paths/register -r ts-node/register \
node_modules/.bin/jest --watchAll --runInBand
Now in VSCode .vscode/launch.json
config add a config to attach to the running process. Note: ensure remoteRoot
fit your setup.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Docker: Debug tests",
"address": "127.0.0.1",
"port": 9229,
"trace": true,
"restart": true,
"timeout": 10000,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"disableOptimisticBPs": true,
"internalConsoleOptions": "neverOpen",
"continueOnAttach": true,
}]
}
From here on I have been able to develop and debug properly my code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With