Debugging Jest on VS Code

强颜欢笑 提交于 2020-04-06 06:54:35

问题


I'm trying to debug Jest unit tests using VS Code. I have the following config file settings

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

However when I run (F5) VS Code I get the following error

Error: AggregatedResult must be present after test run is complete

Any idea why?


回答1:


I can't answer the precise question, however this basic launch config for debugging Jest works for me, it's also simple to include Jest skip files

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



回答2:


About debugging Jest unit tests using VSCode, create a the following file (path: .vscode/launch.json)

If you have created your app using 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"
        }
      ]
    }

If you have created your app from scratch:

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

There are more configurations available, if you need more info check out:

  • Step by step guide (post)
  • Create react app official guide
  • Microsoft official guide



回答3:


@tmp dev, if you simply change runtimeArgs to args in your configuration, it would work:

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

runtimeArgs is to runtimeExecutable as args is to program. As you are launching Jest directly with node, in this case you should use args passing arguments to node. See the Nodejs debugging docs and this ticket for more details.

[2nd way] Specify the actual program to run:

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

[3rd way] If you would like to launch the debugger via npm (which is a runtimeExecutable), and your package.json looks like this:

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

You can launch the debugger using runtimeArgs like this in 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
    }
  ]
}



回答4:


I am using launch.json based on https://www.basefactor.com/using-visual-studio-code-to-debug-jest-based-unit-tests article, in particular example https://github.com/Lemoncode/jest-vs-code-debugging-example/blob/master/custom-solution-jest-config-file/01-implemented/.vscode/launch.json

The section of launch.json to debug single test (assuming that the configuration is located in ./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"
}


来源:https://stackoverflow.com/questions/47563964/debugging-jest-on-vs-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!