83

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?

1
  • 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
    Commented Oct 15, 2016 at 12:42

5 Answers 5

71

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.

11
  • 6
    The most useful answer in the end of 2016
    – Kiril
    Commented Nov 11, 2016 at 14:11
  • 1
    note this is supported as of mocha v3.1.0
    – busticated
    Commented Nov 16, 2016 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
    Commented Nov 17, 2016 at 14:19
  • 1
    @JørgenTvedt I gave it a try but I haven't been able to make --inspect and --watch work together.
    – stropitek
    Commented Dec 15, 2016 at 18:16
  • 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 Commented Aug 17, 2017 at 19:21
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

4
  • 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? Commented Mar 15, 2015 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
    Commented Sep 30, 2015 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! Commented Dec 8, 2015 at 18:00
  • The debug argument got deprecated and replaced by inspect in mocha 7.0 github.com/mochajs/mocha/pull/3890 Commented Mar 14, 2022 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
3
  • thanks - was hoping to just use Node's native interactive debugger, but it seems like node-inspector is the direction most folks have gone. Commented Jan 30, 2013 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? Commented Aug 26, 2014 at 18:27
  • @MedicineMan Add the line debugger; in your code wherever you want a break point. More info here: nodejs.org/api/debugger.html Commented Aug 28, 2014 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

3
  • This is the simplest answer for most people. Commented Sep 3, 2015 at 14:07
  • If you are also using Babel, the babel-node-debug package/command might be helpful.
    – alxndr
    Commented Apr 17, 2016 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
    Commented May 9, 2016 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.