1

I am able to run my Mocha/Unit.js unit test with this command:

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.

enter image description here

Is there a way to execute Mocha unit tests in Eclipse's debug mode?

I am using Eclipse for JavaScript and Web Developers Oxygen Release (4.7.0). Maybe there is a plugin I can install or maybe create a node.js debug configuration and make it run with mocha? Or is there a way to do this in IntelliJ IDE?

javaPlease42
  • 4,699
  • 7
  • 36
  • 65
  • Oxygen release is pretty old. You should upgrade to latest one (2020-06 as I'm writing), which comes with many improvements, including new plugin for JavaScript/TypeScript development. – Mickael Sep 03 '20 at 07:47

1 Answers1

1

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).

Edit: This works in newer versions of eclipse too.

  1. Run your mocha tests
    1. Create a new External Tool Configuration
    2. Set the Location to your mocha.cmd path. For example, if you installed mocha globally, this would be path/to/node/mocha.cmd (in Windows). If you installed locally, then it would be in your node_modules.
    3. Set the Working Directory to your projects path in the workspace. For example ${workspace_loc:/MyNodeProject}
    4. In the Arguments provide the mocha suite path and Node's inspector agent (aka debug) flags: 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). For example dist/tests/_suite.js --inspect-brk (I am using TypeSCript so my tests are in the dist folder).
    5. Run your Configuration (click Run button).
    6. Your Eclipse console should show a message similar to this:
    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.
    1. Create a new Running Node.js Application configuration under Debug Configurations. Menu: 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.
    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.

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

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.

Arcanefoam
  • 687
  • 7
  • 20