VSCode如何使用es6 mocha单元测试和jsx文件进行调试

Mar*_*son 3 javascript mocha.js reactjs visual-studio-code

我设法打破了我的断点,但我遇到了一些问题.

  • 代码在es5中,无法调试
  • 它只用.js命中断点.我需要它用.jsx文件命中.
  • Mocha.opts似乎根本不起作用.我已经尝试--compilers jsx:babel-register从.js 添加和重命名为.jsx,断点不再受到攻击.

似乎阻止它完全运行的摩卡选项:

--require babel-register
--require test/util/dom.js
--require expect
--compilers jsx:babel-register
Run Code Online (Sandbox Code Playgroud)

Launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "request": "launch",
      "name": "Debug Mocha Test",
      "type": "node",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "args": [
        "test/**/*.spec.js", //I need to get this working with .jsx files
        "--require", "babel-register"
        ],
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": null,
      "env": { }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*son 7

事实证明这是节点调试器的一个错误.我通过更改修复了所有问题:

"type": "node""type": "node2".

Launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "request": "launch",
      "name": "Debug Mocha Test",
      "type": "node2",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "args": [
        "test/**/*.spec.jsx",
        "--colors", "--no-timeouts"
        ],
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": null,
      "env": { }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

mocha.opts:

--require babel-register
--require test/util/dom.js
--require expect
--compilers jsx:babel-register
Run Code Online (Sandbox Code Playgroud)

答案取自weinand.

您还需要在根应用程序中使用.babelrc文件"retainLines": true.这是我的.babelrc文件,例如:

{
    "presets": [
        "es2015",
        "stage-2",
        "react"
    ],
    "plugins": [
        "transform-es2015-modules-umd"
    ],
        "retainLines": true
}
Run Code Online (Sandbox Code Playgroud)

如果您获得bad option: --inspect=...,请尝试安装较新版本的节点.