81

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Howard Dierking
  • 1,204
  • 1
  • 11
  • 12
  • Btw there is a difference between `--debug` and `--debug-brk`. The `--debug-brk` stops the application when it starts. The `--debug` starts the application and stops at your first breakpoint. – basickarl Oct 15 '16 at 12:42

5 Answers5

70

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.

stropitek
  • 1,304
  • 1
  • 11
  • 23
  • 6
    The most useful answer in the end of 2016 – Kiril Nov 11 '16 at 14:11
  • I agree Kiril -- works great with the newer versions of Node. Was trying to use `node --inspect --debug-brk ` and it wasn't seeing the debuggers in my test cases. Switching to ` --inspect --debug-brk` solved that real quick! Thanks stropitek. – greg5green Nov 11 '16 at 22:46
  • 1
    note this is supported as of [mocha v3.1.0](https://github.com/mochajs/mocha/blob/master/CHANGELOG.md#310--2016-09-27) – busticated Nov 16 '16 at 20:10
  • 1
    Thanks @busticated. I've added this to the answer, as well as the version of nodejs needed for v8 inspector integration – stropitek Nov 17 '16 at 14:19
  • @stropitek - This helps a lot, thanks! However, I am unable to set breakpoints - at least when running mocha in debugger with --watch. Possibly because the sources are reloaded (unloaded?) for each iteration, chrome disables the breakpoint after the run is completed. Do we have to sneak in a `debugger` statement and then insert breakpoints for each iteration? – Jørgen Tvedt Dec 14 '16 at 09:06
  • 1
    @JørgenTvedt I gave it a try but I haven't been able to make `--inspect` and `--watch` work together. – stropitek Dec 15 '16 at 18:16
  • for some reason `npm test -- --inspect --debug-brk` didn't work for me but `mocha ./test --debug-brk --inspect` did... anyone know why that might be? – tnrich Dec 20 '16 at 23:48
  • @majorBummer difficult to say without further information but maybe you didn't configure a npm test script? https://docs.npmjs.com/misc/scripts – stropitek Dec 21 '16 at 08:12
  • 1
    `npm test -- --inspect --debug-brk` for nodev6.3+ is absolutely correct, I had started to doubt it but as @busticated pointed out, you need to upgrade mocha versions. In my case i went from `3.0.2` to `3.5.0` – pulkitsinghal Aug 17 '17 at 19:21
  • I was able to get it work, wow -- tough experience debugging this! – thedanotto Jan 17 '19 at 22:34
  • As of 7.0.0 / 2020-01-05, this is deprecated in favor of `inspect`, refs https://github.com/mochajs/mocha/blob/v7.0.0/CHANGELOG.md and https://github.com/mochajs/mocha/pull/3890 – Andre Miras Apr 10 '22 at 18:01
58

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

ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
  • 14
    How come using `mocha debug` breaks in `node_modules/mocha/bin/_mocha` instead of in my code where I put the `debugger` statement? How do you get a REPL at the point in your code where you dropped the `debugger` statement? – Nathan Wallace Mar 15 '15 at 14:19
  • 4
    this works (I had to `c` once to get to my breakpoint but NBD.) What's annoying though is I can't seem to inspect anything when sitting at a breakpoint. Like I can have `var foo='bar'; debugger;` and if I type `foo` at the prompt I get `ReferenceError: foo is not defined` – thom_nic Sep 30 '15 at 18:25
  • 9
    The *debugger* command-line is a JavaScript REPL, but it is *not* a REPL instantiated in the same environment as your code. (yes, this is confusing as hell, I know.) You have to remember to type `repl` at the debugger's command-line to get an actual interactive environment for your own code! – ELLIOTTCABLE Dec 08 '15 at 18:00
  • The `debug` argument got deprecated and replaced by `inspect` in mocha 7.0 https://github.com/mochajs/mocha/pull/3890 – Andre Miras Mar 14 '22 at 12:22
51

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
Matthew Shanley
  • 1,091
  • 2
  • 11
  • 14
  • thanks - was hoping to just use Node's native interactive debugger, but it seems like node-inspector is the direction most folks have gone. – Howard Dierking Jan 30 '13 at 17:00
  • 2
    I did this and the debugger started on the first line of mocha source code. I couldn't find a way to open my test and put a breakpoint there. How do I step into my test code conveniently? – MedicineMan Aug 26 '14 at 18:27
  • @MedicineMan Add the line `debugger;` in your code wherever you want a break point. More info here: http://nodejs.org/api/debugger.html – Matthew Shanley Aug 28 '14 at 20:19
10

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

Trideep Gogoi
  • 179
  • 1
  • 4
  • This is the simplest answer for most people. – mikemaccana Sep 03 '15 at 14:07
  • If you are also using Babel, [the `babel-node-debug` package/command](https://www.npmjs.com/package/babel-node-debug) might be helpful. – alxndr Apr 17 '16 at 22:31
  • you may want to run "node-debug _mocha --no-timeouts" to ensure that timeout doesn't happen if you break in a test. – Piran May 09 '16 at 14:57
5

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)

Tom Berghuis
  • 491
  • 3
  • 10
  • 26