What's the right way to enable the node debugger with mocha's --debug-brk switch?

I have some debugger statements in my module under test and want to run mocha with --debug-brk set and hit my breakpoint so that I can inspect the state of my module. Unfortunately, whenever I run mocha with this option, I end up with a blank cursor on the next line. I can enter text, but there's nothing that appears to be processing my commands (it certainly doesn't look like the node debugger):

$ mocha --debug-brk tests.js -R spec
debugger listening on port 5858
[BLANK CURSOR]

Am I doing something wrong with how I'm launching mocha?

51982 次浏览

I was able to get this to work using node-inspector. I run my test like you show in one shell:

mocha --debug-brk mocha/test.js

and then run node-inspector in a second shell:

node-inspector

Bringing up the URL that node-inspector spits out in a browser allows me to debug with the web inspector.

http://127.0.0.1:8080/debug?port=5858

To answer the original question, even though I also suggest you look into node-inspector: you can use the CLI debugger built into node through mocha with the debug option, instead of the --debug or --debug-brk flags. (Notice the lack of dashes.)

In your example, instead, it would be:

$ mocha debug tests.js -R spec
debugger listening on port 5858
connecting... ok
break in node_modules/mocha/bin/_mocha:7
5  */
6
7 var program = require('commander')
8   , sprintf = require('util').format
9   , path = require('path')
debug> [CURSOR]

Again, debug as above in bold, with no dashes. (=

Relevant: https://github.com/visionmedia/mocha/issues/247

If you have node-insector installed you can debug you Mocha tests without actually running node-inspector first. The easiest way is to

node-debug _mocha

That should start debugging the node tests in chrome (also works on Safari)

One reason I have seen that the tests do not work is sometimes you gave to try http://localhost:8080/debug?port=5858 or http://127.0.0.1:8080/debug?port=5858

UPDATE

As of mocha 7.0.0, --debug-brk has been removed in favor of --inspect-brk


Using a recent version of nodejs (>=v6.3.0) and mocha (>=3.1.0), you can use V8 inspector integration.

V8 Inspector integration allows attaching Chrome DevTools to Node.js instances for debugging and profiling

Usage

--inspect activates V8 inspector integration, and --debug-brk adds a breakpoint at the beginning. Since nodejs v7.6.0 and mocha v3.3.0, you can use the --inspect-brk shorthand for --inspect --debug-brk

$ mocha --debug-brk --inspect
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/remote/serve_file/@62cd277117e6f8ec53e31b1be58290a6f7ab42ef/inspector.html?experiments=true&v8only=true&ws=localhost:9229/node

With npm scripts

If you have a npm test script that uses mocha, you can pass the options from npm to your mocha script by using the end of option delimiter --:

$ npm test -- --inspect --debug-brk

Chrome tip

Instead of copy-pasting the url each time, go to chrome://inspect and click the appropriate link in the "Remote target" section.

run mocha with flag --inspect-brk and click 'open dedicated DevTools for node' in chrome from page chrome://inspect. In dedicated DevTools window add connection localhost:9229 to connect automatically.

Also add a debugger statement to the file you want debug.

(this is using latest versions of node and chrome as of October 2017)