VS Code调试器中的Jest + Babel会导致断点移动

Rya*_*sch 32 javascript jestjs babeljs visual-studio-code babel-jest

我正在尝试使用babel,jest和vs代码调试一个简单的项目.当我设置一个断点然后开始调试时,我的断点会跳转并且不再是我开始时的位置.可以在这里看到样本回购 - https://github.com/RyanHirsch/starter-node

我已经更新了我launch.json的内容

{
  "name": "Jest",
  "type": "node",
  "request": "launch",
  "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
  "stopOnEntry": false,
  "args": ["-i", "${file}"],
  "cwd": "${workspaceRoot}",
  "runtimeExecutable": null,
  "sourceMaps": true,
  "protocol": "inspector"
}
Run Code Online (Sandbox Code Playgroud)

.babelrc看起来像:

{
  "plugins": ["@babel/plugin-proposal-object-rest-spread"],
  "sourceMaps": "inline",
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "6.10"
        }
      }
    ]
  ]
}
Run Code Online (Sandbox Code Playgroud)

我认为源映射选项足以让它工作但我错了.需要更改什么才能将断点保留在原始位置?特别是在尝试调试我的测试时.

====编辑====

在断点位于测试线10和实施线4之前:

在调试之前

当我通过选择我的测试文件开始调试然后在VS Code调试窗格中运行Jest时,我的断点跳转到测试第9行和实现第6行: 在调试期间

在节点9.6.1上运行,具有以下扩展名:

DavidAnson.vscode-markdownlint
EditorConfig.EditorConfig
Orta.vscode-jest
PKief.material-icon-theme
PeterJausovec.vscode-docker
Shan.code-settings-sync
bungcip.better-toml
dbaeumer.vscode-eslint
dracula-theme.theme-dracula
dzannotti.vscode-babel-coloring
eamodio.gitlens
esbenp.prettier-vscode
gerane.Theme-FlatlandMonokai
humao.rest-client
mauve.terraform
mikestead.dotenv
mjmcloug.vscode-elixir
mohsen1.prettify-json
ms-vscode.Theme-MaterialKit
ms-vscode.azure-account
ms-vscode.cpptools
ritwickdey.LiveServer
sbrink.elm
shanoor.vscode-nginx
vscodevim.vim
Run Code Online (Sandbox Code Playgroud)

Bra*_*lio 5

遇到了这个问题(使用jest 23.6.0),检查了这是create react app上的一个已知问题,已针对此请求请求解决了该问题:

https://github.com/facebook/create-react-app/pull/3605/files

将以下配置应用于我的launch.json

{ "type": "node", "request": "launch", "name": "Jest All", "program": "${workspaceFolder}/node_modules/jest/bin/jest", "args": [ "test", "--runInBand", "--no-cache"
], "sourceMaps": false, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" },

设法使它在正确的断点处中断。


小智 5

@RyanHirsch; 只需retainLines": truesourceMap: "inline"babelrc 一起使用,它就会起作用。


Shi*_*iva -2

安装节点:

https://nodejs.org/en/download/

安装 Chrome 插件:

https://chrome.google.com/webstore/detail/nodejs-v8-inspector-manag/gnhhdgbaldcilmgcpfddgdbkhjohddkj?hl=en

在您的终端中运行以下脚本

node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand
Run Code Online (Sandbox Code Playgroud)

有关 VS Code 中故障排除的更多参考,请遵循以下说明

https://jestjs.io/docs/en/troubleshooting

 {
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Jest Tests",
      "type": "node",
      "request": "launch",
      "runtimeArgs": [
        "--inspect-brk",
        "${workspaceRoot}/node_modules/jest/bin/jest.js",
        "--runInBand"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Babel 会将 es6 转换为 es5,因此它不依赖于检查。为了进行检查,您需要节点和节点镀铬扩展。

享受编码的乐趣