简体   繁体   English

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

[英]VSCode How do I debug with es6 mocha unit tests and jsx files

I manage to hit my breakpoint, but I have a few problems. 我设法打破了我的断点,但我遇到了一些问题。

  • The code is in es5 and is impossible to debug 代码在es5中,无法调试
  • It only hits the breakpoint with .js. 它只用.js命中断点。 I need it to hit with .jsx files. 我需要它用.jsx文件命中。
  • Mocha.opts doesn't seem to work at all with this. Mocha.opts似乎根本不起作用。 I have tried adding --compilers jsx:babel-register and renaming from .js to .jsx and the breakpoint just doesn't get hit any more. 我尝试添加--compilers jsx:babel-register并从.js重命名为.jsx,断点就不再受到攻击了。

Mocha options that seem to stop it working completely: 似乎阻止它完全运行的摩卡选项:

--require babel-register
--require test/util/dom.js
--require expect
--compilers jsx:babel-register

Launch.json 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": { }
    }
  ]
}

Turns out that this is a bug with the node debugger. 事实证明这是节点调试器的一个错误。 I fixed all of the problems by changing: 我通过更改修复了所有问题:

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

Launch.json 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": { }
    }
  ]
}

mocha.opts: mocha.opts:

--require babel-register
--require test/util/dom.js
--require expect
--compilers jsx:babel-register

Answer taken from weinand . 答案取自weinand

You also need a .babelrc file in your root app with the "retainLines": true . 你还需要一个带有"retainLines": true root应用程序中的.babelrc文件"retainLines": true Here is my .babelrc file for example: 这是我的.babelrc文件,例如:

{
    "presets": [
        "es2015",
        "stage-2",
        "react"
    ],
    "plugins": [
        "transform-es2015-modules-umd"
    ],
        "retainLines": true
}

If you get bad option: --inspect=... , try and installing a newer version of node. 如果您bad option: --inspect=... ,请尝试安装较新版本的节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM