【问题标题】:VSCode debugger not working in Jest testsVSCode 调试器在 Jest 测试中不起作用
【发布时间】:2018-12-03 05:14:06
【问题描述】:

我正在努力让 Visual Studio Code 调试器与 Jest 测试一起工作。

这是我的 launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Jest All",
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "args": ["--runInBand"],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "sourceMaps": true
    }
  ]
}

这是我的带有几个断点的 Jest 测试:

当我点击绿色播放按钮以使用调试器运行测试时,断点永远不会命中。

任何帮助将不胜感激

【问题讨论】:

    标签: visual-studio-code jestjs stenciljs


    【解决方案1】:

    如果您在运行实际测试之前使用像 babelswc 这样的转换器来转换您的测试,则 vscode 中的调试器可能无法工作。

    对我来说,我将只使用debugger

    【讨论】:

      【解决方案2】:

      在尝试了其他所有方法后,以下是唯一对我有用的 launch.config:|

      {
          "version": "0.2.0",
          "configurations": [
          {
            "type": "node",
            "request": "launch",
            "name": "Jest",
            "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
            "args": [
                "-i"
            ],
            "skipFiles": [
                "<node_internals>/**/*.js", "node_modules",
            ]
          }
        ]
      }
      

      【讨论】:

        【解决方案3】:

        对我来说,问题是 launch.json 中 program 属性的值。如果你的launch.json如下:

                "program": "${workspaceFolder}/node_modules/jest/bin/jest"
        

        检查 ${workspaceFolder}/node_modules/jest/bin/jest 是否真的有效。对我来说,node_modules 在这里不存在,而是在workspaceFolder 的子目录中。

        【讨论】:

          【解决方案4】:

          我在关闭行号时遇到了同样的问题。在源文件中,我有将近 30 行需求,并且在调试器中加载的测试文件在每个需求之间添加了一个空格。所以在 vscode 中加载的文件大约长了 60 行。

          我发现这篇文章解决了我的问题:Debugging Jest Tests in VS Code: Breakpoints Move

          【讨论】:

            【解决方案5】:

            我个人使用这个配置

            {
              "name": "Launch e2e test",
              "type": "node",
              "request": "launch",
              "env": {
                "NODE_ENV": "test"
              },
              "args": [
                "--colors",
                "--config=${workspaceFolder}/jest-e2e.config.js",
                "--runInBand",
                "--coverage"
              ],
              "runtimeArgs": [
                "--nolazy"
              ],
              "windows": {
                "program": "${workspaceFolder}/node_modules/jest/bin/jest",
              },
              "outputCapture": "std",
              "internalConsoleOptions": "openOnSessionStart"
            }
            

            通过您的配置文件更改 jest-e2e.config.js。并删除或保留覆盖

            就像 Laura Slocum 说的那样,您肯定会遇到行号问题。就我而言,个人认为问题出在开玩笑的配置上,即转换:

              transform: {
                "^.+\\.(t|j)s$": "ts-jest"
              },
            

            【讨论】:

              【解决方案6】:

              这个配置让我调试玩笑测试。不幸的是,在组件中击中断点不会显示正确的行,即使它正在单步执行正确的代码。我相信这可能是一个 VSCode 错误

              {
                "name": "Jest", // This is the configuration name you will see in debug sidebar
                "type": "node",
                "request": "launch",
                "port": 5858,
                "address": "localhost",
                "stopOnEntry": false,
                "runtimeExecutable": null,
                "env": {
                  "NODE_ENV": "development"
                },
                "console": "integratedTerminal",
                "preLaunchTask": "compile",
                "runtimeArgs": [
                  "--inspect-brk", // node v8 use debug-brk if older version of node
                  "${workspaceRoot}/node_modules/.bin/jest",
                  "--watch",
                  "--bail",
                  "--runInBand"
                ],
                "cwd": "${workspaceRoot}"
              },
              

              【讨论】:

                猜你喜欢
                • 2023-02-07
                • 2018-08-12
                • 1970-01-01
                • 2022-07-19
                • 2020-01-16
                • 1970-01-01
                • 2020-10-30
                • 2020-02-28
                • 2019-12-27
                相关资源
                最近更新 更多