Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Jasmine tests run from Grunt in Visual Studio Code?

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.

like image 450
nwinkler Avatar asked Dec 03 '15 15:12

nwinkler


People also ask

How do I run a grunt file in Visual Studio code?

The most recen update to VSC has auto-detects grunt (and gulp tasks) so you can now just use cmd+p then type task (notice the space at the end) and VSC will show you the available tasks to run. Show activity on this post. You can also modify the existing tasks option to add specific tasks to run in your build.


2 Answers

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.

like image 138
Kevin Friedheim Avatar answered Oct 20 '22 21:10

Kevin Friedheim


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).

like image 23
Andre Weinand Avatar answered Oct 20 '22 21:10

Andre Weinand