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

时间:2018-10-28 08:47:30

标签: node.js docker debugging visual-studio-code jestjs

我最近已从Vim切换到VSCode,并且我试图为通过docker运行的测试进行设置VSCode调试。

调试工作…之类的。如果我想运行笑话测试并激活断点,则需要:

  1. 插入断点
  2. 通过下面的vscode-jest-tests launch.json任务开始运行相关的笑话测试
  3. 在测试套件达到断点之前快速执行Docker: Attach To Node

显然不理想-我很想确保VSCode在运行vscode-jest-tests后自动附加到调试器。简而言之:通过Docker运行Jest测试时,是否有一种简单的方法来附加VSCode调试器?

这是我当前的launch.json和package.json文件。任何帮助都非常感谢:

launch.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"
    }
  ]
}

package.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的调试器就可以正常工作

3 个答案:

答案 0 :(得分:1)

这是我的解决方案的一个镜头,主要来自您的同一问题。谢谢你的提问:)

首先在监视模式 (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 配置中添加一个配置以附加到正在运行的进程。注意:确保 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,
    }]
}

从这里开始,我能够正确地开发和调试我的代码。

答案 1 :(得分: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"
    }
  ]
}

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

答案 2 :(得分:0)

一天后,明白了。

VSCode launch.json

{
  "type": "node",
  "name": "docker-jest",
  "request": "attach",
  "address": "0.0.0.0",
  "port": 9229,
  "localRoot": "${workspaceFolder}",
  "remoteRoot": "/app", // Will depend on your setup
  "skipFiles": [
    "<node_internals>/**/*.js",
    "${workspaceFolder}/node_modules/**/*.js"
  ],
  "internalConsoleOptions": "neverOpen",
  "presentation": {
    "reveal": "silent"
  }
},

确保您的 docker 容器在端口 9229 打开的情况下运行。我使用 tail -d /dev/null 来保持我的运行。我不会详细介绍 docker 设置,因为有很多关于该主题的在线资源。

然后进入docker容器

$ docker exec -it container_name /bin/bash

然后运行

$ node --inspect-brk=0.0.0.0:9229 --nolazy -r ./node_modules/ts-node/register ./node_modules/jest/bin/jest.js --config=./jest-config.json --runInBand

--runInBand 标志 is key 使其起作用。

<块引用>

--runInBand 使所有测试都在父进程而不是子进程中运行。

你应该看到类似的东西

root@7a0ba832e7f9:/app# node --inspect-brk=0.0.0.0:9229 --nolazy -r ./node_modules/ts-node/register ./node_modules/jest/bin/jest.js --config=./jest-config.json --runInBand
Debugger listening on ws://0.0.0.0:9229/267bfe8c-dfa8-45e0-a86c-abcadbd97bea
For help, see: https://nodejs.org/en/docs/inspector

在 Jest .spec.ts 文件中设置断点并执行 docker-jest 调试器。


如果您仍然遇到问题,请确保您的应用尚未在同一端口上以调试模式运行。测试将继续执行,只有短暂的停顿,显示以下内容;

Starting inspector on 0.0.0.0:9229 failed: address already in use

如果发生这种情况,只需将 node 命令和 launch.json 中使用的调试端口更改为其他内容并打开 docker 端口。

相关问题