node.js - VSCode 新手 : Remote Jest/Node debugging through Docker

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

我最近从 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/

    相关文章:

    Node.js 和 Redis;等待循环结束

    node.js - Node - async.map 如何工作?

    使用 'DB_HOST=mysql' 时终端中的 Laravel WSL Docker MYSQL 连接问题

    amazon-web-services - AWS 教程 - 将 Docker 镜像推送到 AWS ECR 错误 x509

    python - 有没有办法使用 pydev 调试子进程?

    node.js - node.js 中 readFileSync 的错误处理

    docker - 构建派生图像时无法在缓存中找到(本地)图像

    c++ - 如何让程序 D 读取分配给程序 A 的内存中的内存位置?

    Django:manage.py loaddata 的更好调试消息

    node.js - 在 Kubernetes 中连接前端和后端