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.
- Run your mocha tests
- Create a new External Tool Configuration
- 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
.
- Set the Working Directory to your projects path in the workspace. For example
${workspace_loc:/MyNodeProject}
- 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).
- Run your Configuration (click Run button).
- 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
- Start a Wild Web Developers inspector client.
- Create a new Running Node.js Application configuration under Debug Configurations. Menu:
Run -> Debug Configurations...
- 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.
- Start the debug session (click Debug button)
- 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.