9

I am using WebStorm to run a grunt task. The debugger successfully stops at the breakpoint in the Gruntfile.js file, but not in my task file.

In the Gruntfile.js I register a task like this:

grunt.initConfig({
  ... configuration ...
});    
grunt.registerTask('myTask', ['mocha:myTask']);

When I set a breakpoint in the corresponding js file for the test 'myTask' it doesn't stop. How can I debug also the grunt tests?


--- UPDATE ---------------------------------------

so i tried all of your possible solutions, but it does not solve my problem!

I am able to debug the grunt script itself, this is where the debugger actually stops (either in WebStorm or node-inspector). Also a breakpoint in Gruntfile.js is working.

The problem is, that I am not able to debug the actual Grunt task itself, registered with grunt like this: grunt.registerTask('myTask', ['mocha:myTask']);

I am also able to debug the mocha test itself. But I want to debug a mocha test called from grunt task runner. Any ideas?

electronix384128
  • 6,625
  • 11
  • 45
  • 67

4 Answers4

6

To run grunt task in debug, you need to pass the grunt task script to node-inspector:

node-debug $(Path \AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt) task

Put a debugger; line in your task. node-inspector will then open a browser with debugging tools.

This link help you how its work grunt-node-inspector

Example: ChrisWren/grunt-node-inspector

Source: Stackoverflow Question

Community
  • 1
  • 1
Prateek
  • 6,785
  • 2
  • 24
  • 37
  • Thanks for your answer. Unfortunately it does not work. I placed "debugger;" on the first line of the task.js file, but its not even visible anymore in the node-inspector and it is also not stopping. – electronix384128 Sep 29 '14 at 13:01
  • @BenMarten Just take a look **[debugging-with-node-inspector](http://elegantcode.com/2011/01/14/taking-baby-steps-with-node-js-debugging-with-node-inspector/)** – Prateek Oct 08 '14 at 11:26
2

Finally I am able to debug my mocha tasks! Thanks for all your answers and comments, but unfortunately I have to select my own answer, because that is the only one that worked for me.

Thanks to this video I found out what I was missing: http://vimeo.com/97561531

Basically it was two things:

  1. Add a "debug-brk" option to grunt's mocha configuration:

    grunt.initConfig({
        ...
            "options": {
                "mocha": {
                    ...
                    "debug-brk": (grunt.option('debug-brk')) ? "" : 0
                }
            }
    }
    
  2. Configure WebStorm's Debug Configuration like this:

    WebStorm's Debug Config to Halt on debug-brk

electronix384128
  • 6,625
  • 11
  • 45
  • 67
  • 6
    How awesome that you found my video in solving your problem. Awesomer still, I was having an issue with debugging in web storm and newer versions of mocha. In googling for this issue I came across YOUR answer. Your small modification to the code I'd written earlier answered MY question… CHEERS!!!! – Matt Dec 11 '14 at 22:39
1

If you happen to use the WebStorm IDE you can set up a task and then either run or debug it.

You can see the configuration for the command grunt jasmine_node_no_coverage in the screenshot. Please note that I installed grunt globally.

Configuration of the task in WebStorm

analog-nico
  • 2,750
  • 17
  • 25
  • Do I understand correctly that you want to debug the code of grunt-mocha? If yes, then you need to set a break point in these sources. You should be able to find a line like `grunt.registerTask("mocha", function () { ... });`. The function is executed when you run a mocha task. – analog-nico Oct 08 '14 at 15:18
  • I only have a project that uses grunt with grunt-jasmine-node-coverage. And there it was stopping inside the function when I tried it out earlier today. I set the breakpoint in `./node_modules/grunt-jasmine-node-coverage/tasks/jasmine-node-task.js` on the first line of the function passed to registerTask: `grunt.registerTask("jasmine_node", "Runs jasmine-node.", function () { /* HERE */ var jasmine = require('jasmine-node'); /* ... */ });` – analog-nico Oct 08 '14 at 19:40
  • Yes sorry it is actually stopping. When I replace ['mocha:myTask'] with a real function and place the debugger within the function, it works, but not in mocha myTask.js file ... – electronix384128 Oct 08 '14 at 19:41
0

Try out grunt-debug-task.

Its similar to node debugger. Just run the grunt debug taskname

I tried it out. It breaks sometimes but seems to work.

Sami
  • 3,926
  • 1
  • 29
  • 42