【问题标题】:Debugging Jest on VS Code在 VS Code 上调试 Jest
【发布时间】:2018-05-13 19:43:44
【问题描述】:

我正在尝试使用 VS Code 调试 Jest 单元测试。我有以下配置文件设置

"configurations": [
    {
        "name": "Debug Jest Tests",
        "type": "node",
        "request": "launch",
        "runtimeArgs": [
            "--inspect-brk",
            "${workspaceRoot}/node_modules//jest/bin/jest.js",
            "--runInBand"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen"
    }
]

但是,当我运行 (F5) VS Code 时,出现以下错误

错误:测试运行完成后必须存在 AggregatedResult

知道为什么吗?

【问题讨论】:

    标签: debugging visual-studio-code jestjs


    【解决方案1】:

    我无法回答确切的问题,但是这个用于调试 Jest 的基本启动配置对我有用,包含 Jest 跳过文件也很简单

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

    【讨论】:

      【解决方案2】:

      关于使用VSCode调试Jest单元测试,创建如下文件(路径:.vscode/launch.json

      如果您使用 create-react-app

      创建了您的应用
        {
            "version": "0.2.0",
            "configurations": [
              {
                "name": "Debug tests watch mode",
                "type": "node",
                "request": "launch",
                "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
                "args": ["test", "--runInBand", "--no-cache", "--watchAll=true"],
                "cwd": "${workspaceRoot}",
                "protocol": "inspector",
                "console": "integratedTerminal",
                "internalConsoleOptions": "neverOpen"
              }
            ]
          }
      

      如果您是从头开始创建应用的:

         {
            "version": "0.2.0",
            "configurations": [
              {
                "type": "node",
                "request": "launch",
                "name": "Jest watch all tests",
                "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
                "args": [
                  "--verbose",
                  "-i",
                  "--no-cache",
                  "--watchAll"
                ],
                "console": "integratedTerminal",
                "internalConsoleOptions": "neverOpen"
              }
            ]
          }
      

      还有更多配置可供选择,如果您需要更多信息,请查看:

      【讨论】:

        【解决方案3】:

        @tmp 开发人员,如果您只是在配置中将 runtimeArgs 更改为 args,它将起作用:

        "configurations": [
            {
                "name": "Debug Jest Tests",
                "type": "node",
                "request": "launch",
                "args": [
                    "${workspaceRoot}/node_modules/jest/bin/jest.js",
                    "--runInBand"
                ],
                "console": "integratedTerminal",
                "internalConsoleOptions": "neverOpen"
            }
        ]
        

        runtimeArgs 是 runtimeExecutable 就像 args 是编程。当您直接使用节点启动 Jest 时,在这种情况下,您应该使用 args 将参数传递给 node。有关详细信息,请参阅 Nodejs debugging docsthis ticket

        [2nd way]指定实际运行的程序:

          "configurations": [
            {
              "type": "node",
              "request": "launch",
              "name": "Jest Test",
              "program": "${workspaceFolder}/node_modules/jest/bin/jest",
              "args": ["--runInBand", "--config=${workspaceFolder}/jest.config.js"],
              "console": "integratedTerminal",
              "internalConsoleOptions": "neverOpen"
            }
          ]
        

        [第三种方式]如果您想通过 npm(它是一个 runtimeExecutable)启动调试器,并且您的 package.json 看起来像这样:

        {
          "scripts": {
            "test:unit:debug": "node --inspect-brk=9229 ./node_modules/jest/bin/jest.js --no-cache --runInBand"
          },
          ...
        }
        

        您可以在 VS Code 中像这样使用runtimeArgs 启动调试器:

        {
          "version": "0.2.0",
          "configurations": [
            {
              "name": "Launch via npm",
              "type": "node",
              "request": "launch",
              "cwd": "${workspaceFolder}",
              "runtimeExecutable": "npm",
              "runtimeArgs": ["run-script", "test:unit:debug"],
              "port": 9229
            }
          ]
        }
        

        【讨论】:

          【解决方案4】:

          我正在使用基于 https://www.basefactor.com/using-visual-studio-code-to-debug-jest-based-unit-tests 文章,特别是https://github.com/Lemoncode/jest-vs-code-debugging-example/blob/master/custom-solution-jest-config-file/01-implemented/.vscode/launch.json

          launch.json中调试单次测试的部分(假设配置位于./config/test/jest.json):

          {
            "type": "node",
            "request": "launch",
            "name": "Jest debug current file",
            "program": "${workspaceFolder}/node_modules/jest/bin/jest",
            "args": [
              "${fileBasename}",
              "-c",
              "./config/test/jest.json",
              "--verbose",
              "-i",
              "--no-cache",
              //"--watchAll"
            ],
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
          }
          

          【讨论】:

            猜你喜欢
            • 2018-08-20
            • 2019-02-10
            • 1970-01-01
            • 2021-12-19
            • 2020-03-16
            • 1970-01-01
            • 2020-08-25
            • 1970-01-01
            • 2018-08-06
            相关资源
            最近更新 更多