在VS代码上调试Jest

时间:2017-11-30 00:15:53

标签: debugging visual-studio-code jestjs

我尝试使用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

知道为什么吗?

4 个答案:

答案 0 :(得分:0)

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

"))"

答案 1 :(得分:0)

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

如果您是使用 create-react-app

创建的应用
ERROR: Could not find a version that satisfies the requirement pylint==2.3.1 (from pre-commit-dummy-package==0.0.0)

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

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

还有更多可用的配置,如果您需要更多信息,请查看:

答案 2 :(得分:0)

@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对于runtime.Executable就像args是编程一样。在直接使用node启动Jest时,在这种情况下,应使用args传递参数到node。有关更多详细信息,请参见Nodejs debugging docsthis ticket

[第二种方式] 指定要运行的实际程序:

  "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"
  },
  ...
}

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

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

答案 3 :(得分:0)

我正在使用基于 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"
}
相关问题