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

后端 未结 10 478
夕颜
夕颜 2020-12-12 17:18

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.

相关标签:
10条回答
  • 2020-12-12 17:40

    In Webstorm now you can just set up using a mocha configuration. Worked pretty much out of the box for me:

    Node interpreter: /usr/local/bin/node
    Working directory: /Users/me/sites/mysite
    Mocha Package: /Users/me/sites/mysite/node_modules/mocha
    

    and then

    All in directory
    Test directory: /Users/me/sites/mysite/test
    

    It also shows you the parameters it runs with so you can probably copy them to another environment if you need to.

    0 讨论(0)
  • 2020-12-12 17:45

    A modern way to do this is to use nodejs's inspector integration feature. It's fairly simple to use. I've already written a detailed explanation of how to use it in this post

    0 讨论(0)
  • 2020-12-12 17:51

    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

    0 讨论(0)
  • 2020-12-12 17:52

    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

    0 讨论(0)
  • 2020-12-12 17:54

    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.

    0 讨论(0)
  • 2020-12-12 17:55

    In regards to either Webstorm or PhpStorm, you can add a specific mocha debug configuration:

    Debug configuration

    You'll have to add via the green, you might give it a name.

    If the already installed mocha in the project via:

     npm install mocha --save
    

    or

     yarn add mocha
    

    it will find the according module in your project.

    I had to provide the correct path to my unit tests, and hit the mark for Include subdirectories/

    Since my project is a typescript one I had to add:

    yarn add ts-node
    

    For a pure js project it should not be necessary.

    Now you can run the entire test suit, and you can then pick single test cases from the list and run them on and debug them on their own.

    0 讨论(0)
提交回复
热议问题