简体   繁体   English

如何在使用 Mocha 运行的 Eclipse 中调试 Javascript 单元测试?

[英]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).我假设您已经在 J​​avaScript 文件中放置了所需的断点(如果您使用的是 TypeScript,请将断点放置在已编译的 JavaScript 文件中)。

Edit: This works in newer versions of eclipse too.编辑:这也适用于较新版本的 eclipse。

  1. Run your mocha tests运行你的 mocha 测试
    1. Create a new External Tool Configuration创建新的外部工具配置
    2. Set the Location to your 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
    3. Set the Working Directory to your projects path in the workspace.工作目录设置为工作区中的项目路径。 For example ${workspace_loc:/MyNodeProject}例如${workspace_loc:/MyNodeProject}
    4. In the Arguments provide the mocha suite path and Node's inspector agent (aka debug) flags: 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 文件夹中)。
    5. Run your Configuration (click Run button).运行您的配置(单击运行按钮)。
    6. Your Eclipse console should show a message similar to this:您的 Eclipse 控制台应显示类似于以下内容的消息:
     Debugger listening on ws://127.0.0.1:9229/229a0421-cc41-4202-ab46-a392e86418e3 For help, see: https://nodejs.org/en/docs/inspector
  2. Start a Wild Web Developers inspector client.启动 Wild Web Developers 检查器客户端。
    1. Create a new Running Node.js Application configuration under Debug Configurations .Debug Configurations下创建一个新的Running Node.js Application配置 Menu: Run -> Debug Configurations...菜单: Run -> Debug Configurations...
    2. In the Attach tab set the Address and Port to the values printed in the console (from step one).附加选项卡中,将地址端口设置为控制台中打印的值(从第一步开始)。 In this case Address : 127.0.0.1 (equivalent to localhost), and Port : 9229. These are the default values, so if you have not changed your node/mocha configuration you should be using the same.在这种情况下,地址:127.0.0.1(相当于本地主机)和端口:9229。这些是默认值,所以如果你没有改变你的节点/摩卡配置,你应该使用相同的。
    3. Start the debug session (click Debug button)启动调试会话(单击调试按钮)
    4. Your Eclipse should switch to the debug perspective and your test code start executing till the first break point.您的 Eclipse 应该切换到调试透视图并且您的测试代码开始执行直到第一个断点。

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).进行更改后,由于您已经拥有运行配置,您只需要启动它们(顺序无关紧要,因为检查器客户端调试配置将“无限期”运行,等待远程应用程序连接)。

Issues with Node 10节点 10 的问题

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.

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