[英]How do I Debug a Javascript Unit Test in Eclipse that is Run with Mocha?
I am able to run my Mocha/Unit.js unit test with this command:我可以使用以下命令运行我的Mocha/Unit.js单元测试:
mocha fooTest.js
I want to set breakpoints in Eclipse and debug this unit test but I only see the following Debug Configuration options in Eclipse.我想在 Eclipse 中设置断点并调试此单元测试,但我在 Eclipse 中只看到以下调试配置选项。
Is there a way to execute Mocha unit tests in Eclipse's debug mode?有没有办法在 Eclipse 的调试模式下执行 Mocha 单元测试?
I am using Eclipse for JavaScript and Web Developers Oxygen Release (4.7.0) .我正在为 JavaScript 和 Web Developers Oxygen Release (4.7.0)使用Eclipse 。 Maybe there is a plugin I can install or maybe create a node.js debug configuration and make it run with mocha?也许我可以安装一个插件,或者创建一个 node.js 调试配置并使其与 mocha 一起运行? Or is there a way to do this in IntelliJ IDE?或者有没有办法在 IntelliJ IDE 中做到这一点?
Its a two step process.它是一个两步过程。 I assume you have already placed the desired breakpoints on your JavaScript files (if you are using TypeScript, place the breakpoints in the compiled JavaScript files).我假设您已经在 JavaScript 文件中放置了所需的断点(如果您使用的是 TypeScript,请将断点放置在已编译的 JavaScript 文件中)。
Edit: This works in newer versions of eclipse too.编辑:这也适用于较新版本的 eclipse。
mocha.cmd
path.将位置设置为您的mocha.cmd
路径。 For example, if you installed mocha globally, this would be path/to/node/mocha.cmd
(in Windows).例如,如果您全局安装 mocha,这将是path/to/node/mocha.cmd
(在 Windows 中)。 If you installed locally, then it would be in your node_modules
.如果您在本地安装,那么它将在您的node_modules
。${workspace_loc:/MyNodeProject}
例如${workspace_loc:/MyNodeProject}
inspect-brk
.在参数中提供 mocha 套件路径和节点的检查器代理(又名调试)标志: inspect-brk
。 The inspect-brk
flag is important as it Breaks before user code starts , making your mocha tests to break/stop until you attach your debugger (step 2). inspect-brk
标志很重要,因为它在用户代码开始之前中断,使您的 mocha 测试中断/停止,直到您附加调试器(第 2 步)。 For example dist/tests/_suite.js --inspect-brk
(I am using TypeSCript so my tests are in the dist folder).例如dist/tests/_suite.js --inspect-brk
(我使用的是 TypeSCript,所以我的测试在 dist 文件夹中)。 Debugger listening on ws://127.0.0.1:9229/229a0421-cc41-4202-ab46-a392e86418e3 For help, see: https://nodejs.org/en/docs/inspector
Run -> Debug Configurations...
菜单: Run -> Debug Configurations...
After doing changes, since you already have the run configurations you just need to launch them (order is irrelevant as the inspector client debug config will run "indefinitely" waiting for a remote application connection).进行更改后,由于您已经拥有运行配置,您只需要启动它们(顺序无关紧要,因为检查器客户端调试配置将“无限期”运行,等待远程应用程序连接)。
According to this question , there is a bug in node which causes the debugger to fail for child processes.根据这个问题,节点中存在一个错误,导致子进程的调试器失败。 Since mocha runs the tests in child processes, if you are using Node 10, you wont be able to debug your actual test code.由于 mocha 在子进程中运行测试,如果您使用的是 Node 10,您将无法调试您的实际测试代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.