我最近从 Vim 切换到 VSCode,我正在尝试为通过 docker 运行的 jest 测试设置 VSCode 调试。
调试工作......有点。如果我想运行 Jest 测试并激活断点,我需要:
vscode-jest-tests
开始运行相关的笑话测试下方的launch.json任务Docker: Attach To Node
在测试套件遇到断点之前 显然不理想 - 我很想确保 VSCode 在运行时自动附加到调试器
vscode-jest-tests
.简而言之:通过 Docker 运行 Jest 测试时,是否有一种简单的方法可以附加 VSCode 调试器?这是我当前的 launch.json 和 package.json 文件。非常感谢任何帮助:
启动文件
{
"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"
}
]
}
包.json
#...
"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:如果我运行
npm run test:debug
从命令行并打开一个 chrome 调试器窗口,Chrome 的调试器工作正常
最佳答案
这是我的解决方案的一个镜头,主要来自您的同一问题。谢谢你的慰问 :)
首次启动 jest
在监视模式 ( --watchAll
) 中,进程保持事件状态。 (在代码片段中,我假设 backend
容器通过 docker-compose
运行,端口 9229
暴露在主机上)
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
现在在 VSCode .vscode/launch.json
config 添加一个配置以附加到正在运行的进程。注意:确保 remoteRoot
适合您的设置。{
// 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,
}]
}
从这里开始,我已经能够正确地开发和调试我的代码。
关于node.js - VSCode 新手 : Remote Jest/Node debugging through Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53029779/