what's the least resistance path to debugging mocha tests?

23,588

Solution 1

Edit, years later: the shortest path in Node 6+ is: mocha --debug-brk --inspect ./test.js coupled with the Node Inspector Manager plugin.

Many weeks later, no answers. Here's the quickest path that I found.

  1. write mocha tests
  2. install node-inspector
  3. start node-inspector -- it will now be listening on 5858
  4. start the mocha test with --debug-brk
  5. at this point the mocha test is paused on the first line
  6. open a web browser and go to localhost:5858
  7. (optional: add a debugger line at the top of your test file, set breakpoints after it stops in that file)
  8. hit F10 to get the code to go
  9. node-inspector will stop on any line that has debugger on it. Occasionally it won't move the code file's window to the right place, so you'll have to hit F10 to get it to step to the next line and show where it's at in the file.

Command line:

node-inspector & mocha --compilers coffee:coffee-script/register ./test/appTests.coffee --ui bdd -d -g "should X then Y" --debug-brk

Solution 2

In addition to @jcollum's answer above, I have found instead of using the --debug-brk flag, it is better to just use the --debug flag with -w (watch)

That way, when you add and remove debugger lines from your code, mocha will reload the tests automatically and your node-inspector will pause on the appropriate line.

This saves having to revisit the terminal constantly restarting the tests, then needlessly hitting "continue" in the debugger to get past the first line of the source.

Solution 3

With the latest versions of Mocha and node-inspector, this has been working great for me:

$ node-debug ./node_modules/mocha/bin/_mocha

It loads up the local Mocha executable as the debugged process, stopping on the first line for you to set up your breakpoints.

Solution 4

Heads up on http://s-a.github.io/iron-node/. This is most efficient software to debug anything Node.js related.

$ iron-node ./node_modules/mocha/bin/_mocha

enter image description here

enter image description here

Solution 5

The alternative way using WebStorm node debugger.

In short:

  1. You need WebStorm
  2. Create new Node debug profile in WebStorm
  3. Set path to your mocha binary into Path to Node App JS File
  4. Add breakpoints and start the session from WebStorm

Detailed instruction with screenshots by Glenn Block.

Share:
23,588

Related videos on Youtube

jcollum
Author by

jcollum

Updated on July 09, 2022

Comments

  • jcollum
    jcollum almost 2 years

    Edit Nov 2016: Node now has a built in debugger that you can start with --inspect. This answer explains it: https://stackoverflow.com/a/39901169/30946.

    I'm building a mocha test in coffeescript. Right at the top of the test I have:

    require "../assets/js/theObject.coffee"
    debugger
    ss = new TheObject()
    

    I'd like to stop on that debugger line because the object in theObject.coffee isn't being loaded. I'm using node-inspector and it works, sorta.

    The process that I have is:

    1. start node-inspector
    2. run the test at the command line with mocha --compilers coffee:coffee-script ./test/theObjectTests.coffee --ui bdd -d --debug-brk
    3. go to the node-inspector page, refresh it if it is already open
    4. wait for the file theObject.coffee to be loaded, then put a breakpoint on the correct line

    There must be an easier way. It seems like I should be able to have a debugger running and just have it stop on that debugger line, but I'm not able to find that.

    I have WebStorm, which has a debugger (this article discusses setting it up to run mocha tests, but it didn't help me), but when I start it, it fails. The command that's running in the WebStorm debug window is:

    "C:\Program Files\nodejs\node.exe" --debug-brk=64232 C:\Users\jcollum\AppData\Roaming\npm\_mocha
    
    C:\Users\jcollum\AppData\Roaming\npm\_mocha:2
    basedir=`dirname "$0"`
    

    I suspect that might be a windows specific issue.

    Env: Windows 7, Webstorm, node 0.8.16, mocha 1.7.4, git-bash

    The question: if you're starting from scratch with Mocha, what's the easiest way to get a debugger going that will stop on a debugger line easily? Easy is the keyword here.

    Edit: since asking this I've stopped using Windows and am working in Ubuntu. My mocha debugging process (which I use infrequently) is the same.

  • captainclam
    captainclam about 11 years
    this is the best I've found too. If you ever find a better way please update this answer :)
  • jcollum
    jcollum over 10 years
    I tried this when I first got Webstorm. Couldn't get it to work, gave up. I found that blog post a long time ago.
  • Brock
    Brock over 10 years
    Odd, guess bad luck. Worked like a charm for me lately, and I really liked how easy it was to setup. Consider giving it one more try :)
  • Dmitry Matveev
    Dmitry Matveev over 10 years
    the idea of a test is for it to be as granular, small and easy to write as possible so that it is readily obvious what is the logic behind the test. If you find yourself needing to debug your tests you are doing it wrong... period. Find a way to break down tests into very small chunks or you will not ever finish testing and whatever you did manage to test will most probably be too unreliable for anyone to tell that software is ready for production.
  • jcollum
    jcollum over 10 years
    @DmitryMatveev sometimes mocha doesn't fail, it just sits there. Debugging is helpful for that. You're assuming a lot about my code based on my question.
  • Dmitry Matveev
    Dmitry Matveev over 10 years
    @jcollum don't get me wrong 'j'. this is just what I believe about how tests should be. And yes the situation you described is familiar to me, and guess how I made sure that it is not the problem in a test :) pure simplicity of it. just by looking at it (note, not debugging it) it became clear that it was in fact the application being tested that was not returning the response resulting in mocha tests sitting there forever. From my experience I am advocating the point: make em easy and avoid testing to test that your tests actually test
  • Derek Prior
    Derek Prior about 10 years
    @DmitryMatveev You're describing a unit test. There are several other types of tests where debugging is very handy.
  • John Galambos
    John Galambos over 9 years
    I was having trouble getting jcollums solution working in OS X. The source and spec files didn't seem to load in node-inspector. This worked for me.
  • Kieran Ryan
    Kieran Ryan over 8 years
    @Dmitry - even small and easy tests can be misinterpreted - debugger can confirm one's expectations.
  • Dmitry Matveev
    Dmitry Matveev over 8 years
    @KieranRyan - back to my point and I feel like I made a good amount of testing at work to stand my point... if the test can be misinterpreted then it is a badly designed test.. that's it, there is no other alternative... redesign your tests in such a way that there is absolutely no doubt about what a single test case is supposed to be testing.
  • max
    max over 8 years
    @DmitryMatveev - so if I'm reading this correctly, in a perfect world, no one needs debuggers because everyone always TDDs everything, and they do it all "correctly". I completely agree. Well, time to go back to the real world where I try to spend a few hours each day!
  • jcollum
    jcollum over 8 years
    Could you elaborate? What's better with this vs. node-inspector?
  • Stephan Ahlf
    Stephan Ahlf over 8 years
    just a few things... ironNode does support themes. ironNode does not use WebSockets or something like this. (No connection lost messages or something like that). ironNode works independent of local node installation because it has its own. No too fast scripts to attach the debugger problems. No --debug-brk or other preperation necessary to start a debug session. No UI in a weird states behavior. NO UI doesn't load or doesn't work and refresh didn't help behavior No debugger takes a long time to start up behavior. :) just try it. I use exclusively
  • Admin
    Admin about 8 years
    I get Error: Injection failed: no require in current frame. with --debug. Even tried it with --no-preload, but still have to press Pause and Play.
  • Peter Majeed
    Peter Majeed about 8 years
    This is so much faster for me than node-inspector. Thanks for posting this.
  • jcollum
    jcollum over 7 years
    Updated the question.
  • k0pernikus
    k0pernikus almost 7 years
    The blog post it outdated. I had more success watching this example setup: youtube.com/watch?v=4mKiGkokyx8&feature=youtu.be
  • Eric Blade
    Eric Blade almost 6 years
    Of course, when a test completely fails and you have absolutely no idea what code is throwing the error, it's helpful to be able to fire up the debugger, and see where the exception occurred, even though it's being caught. And you can do that with, say Visual Studio Code, just by configuring the debugger to launch Mocha, and selecting "All Exceptions" or "Uncaught Exceptions" before pressing F5 to start. I stick an "only" on the test case that is erroring out, then press F5, and it drops me right at the line that errored.
  • timhc22
    timhc22 almost 6 years
    Yes! @Tree it will stop there