VSCode 新手:通过 Docker 进行远程 Jest/Node 调试

Pla*_*Ton 8 debugging node.js docker jestjs visual-studio-code

我最近从 Vim 切换到 VSCode,我正在尝试为通过 docker 运行的 jest 测试设置 VSCode 调试。

调试工作......有点。如果我想运行 Jest 测试并激活断点,我需要:

  1. 插入断点
  2. 通过vscode-jest-tests下面的launch.json 任务开始运行相关的 jest 测试
  3. 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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

包.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"
}
#...
Run Code Online (Sandbox Code Playgroud)

PS:如果我从命令行运行npm run test:debug并打开一个 chrome 调试器窗口,Chrome 的调试器就可以正常工作

小智 0

这是我的 launch.json 配置,用于使用 Docker 调试 Jest 测试。

{
  // 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",
      "name": "Docker: Jest Tests",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/.bin/jest",
      "args": [
        "--runInBand"
      ],
      "cwd": "${workspaceFolder}",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "disableOptimisticBPs": true,
      "windows": {
        "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      },
      "protocol": "inspector"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这个想法是在通过 VSCode 运行和调试之前启动 docker-compose,其中包括一个数据库(Postgres、MongoDB 等)和一个 api 应用程序。