VSCode新手:通过Docker进行远程Jest/Node调试 [英] VSCode Newbie: Remote Jest/Node debugging through Docker

查看:79
本文介绍了VSCode新手:通过Docker进行远程Jest/Node调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从Vim切换到VSCode,并且我试图为通过docker运行的开玩笑的测试设置VSCode调试.

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:

  1. 插入断点
  2. 通过下面的 vscode-jest-tests launch.json任务开始运行相关的笑话测试
  3. 在测试套件达到断点之前快速执行 Docker:Attach to Node
  1. Insert the breakpoints
  2. Start running the relevant jest test(s) via the vscode-jest-tests launch.json task below
  3. Quickly execute Docker: Attach To Node before the test suite hits breakpoints

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

Obviously 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?

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

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

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运行test:debug 打开Chrome调试器窗口,Chrome的调试器就可以正常工作

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 :)

首先在监视模式(-watchAll )中启动 jest ,这样该过程仍然有效.(在该代码段中,我假设一个 backend 容器通过 docker-compose 在主机上暴露了端口 9229 的情况下运行)

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 

现在,在VSCode .vscode/launch.json 配置中添加一个配置以附加到正在运行的进程.注意:确保 remoteRoot 适合您的设置.

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.

这篇关于VSCode新手:通过Docker进行远程Jest/Node调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆