12

My unit tests are run using Karma/Jasmine through Grunt. When I run

grunt test

the tests are executed from command line.

When opening the project in Visual Studio Code, I can run the same command using Tasks: Run Test Task. VSCode executes Grunt with the test parameter and shows the output.

How can I debug the test cases that are run by VSCode in this case? When I press F5, the launch.json template file is opened. What do I need to provide for program, args etc. to start/debug the same test cases that are run by grunt test?

I have tried the following:

  • program: /usr/local/bin/grunt
  • args: ["test"]

This successfully starts the Grunt process and the tests are executed, but it does not stop at the breakpoints in my test code.

In addition to that, it closes (or crashes) the whole VSCode process after a couple of seconds. Not sure whether that's a bug in VSCode or a result of the above run configuration.

nwinkler
  • 52,665
  • 21
  • 154
  • 168

2 Answers2

4

I don't think you can currently do something like node --debug-brk grunt test where test will spin up jasmine tests - since jasmine will invoke node on these spec files without the debug flag in place. I tried this and here is what I got:

node --debug-brk=3691 --nolazy ../../../usr/local/bin/grunt kftest --schema=9.2.1 --dbtype=sqlite --target=builder/properties --spec=test/builder/properties/properties-spec.js 
Debugger listening on port 3691
Running "kftest" task
>> going to run with spec:  test/builder/properties/properties-spec.js
>> command: node --debug-brk=46307 /Users/computername/project/node_modules/jasmine-node/lib/jasmine-node/cli.js test/builder/properties/properties-spec.js
Running "shell:kftest" (shell) task
Debugger listening on port 46307

This isn't too helpful since now vscode's debugger will be looking at 3691 while 46307 is not being inspected by anything - and I don't know how to tell vscode to also listen to that port.

Soooo what I ended up doing was to follow the answer posted here: Debugging jasmine-node tests with node-inspector

Basically my vscode launch.json included a config that looked like this:

{
  "name": "Jasmine-Node Debugging",
  "cwd": "${workspaceRoot}",
  "program": "${workspaceRoot}/node_modules/jasmine-node/lib/jasmine-node/cli.js",
  "request": "launch",
  "type": "node",
  "args": [
    "test/builder/properties/properties-spec.js"
  ]
}

Hope that helps.

Community
  • 1
  • 1
Kevin Friedheim
  • 394
  • 3
  • 15
2

This launch config works for me in VS Code 0.10.2:

{
    "name": "grunt",
    "type": "node",
    "request": "launch",
    "program": "/usr/local/bin/grunt",
    "args": ["test"],
    "stopOnEntry": false
}

Setting a breakpoint in my "test" task made the VS Code debugger to stop there. I had to install grunt locally (in the folder where I have the Gruntfile).

Andre Weinand
  • 1,907
  • 11
  • 8
  • I've tried that config - and it closes the whole VS Code app once the test execution finishes... – nwinkler Dec 10 '15 at 13:20
  • I'm also trying to figure this out now - it looks like im able to debug the run code in the `Gruntfile.js` but nothing outside of that... which is what I think me and @nwinkler want. – Kevin Friedheim Aug 25 '16 at 21:32