Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Karma tests in Visual Studio Code?

I want to debug Karma tests in VS Code but I did not find out how. Is there any way to do that or will I have to use another IDE (WebStorm)?

like image 664
geo Avatar asked Mar 15 '17 08:03

geo


People also ask

How do I Debug a test code in Visual Studio?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do you Debug Jasmine karma?

Jasmine debug runner Another helpful feature is Karma's debug test runner. Click on the big “DEBUG” button on the top-right. Then a new tab opens with http://localhost:9876/debug.html.


2 Answers

You can debug Karma by attaching the debugger to a Chrome instance. You'd want to set your launch.json config to something like this:

{     "version": "0.2.0",     "configurations": [         {             "type": "chrome",             "request": "attach",             "name": "Attach Karma Chrome",             "address": "localhost",             "port": 9333,             "pathMapping": {                 "/": "${workspaceRoot}/",                 "/base/": "${workspaceRoot}/"             }         }     ] } 

But you also need to adjust your karma.conf.js config, so that it launches Chrome instance with dev tools listening to 9333 port, like so:

browsers: [   'ChromeDebugging' ],  customLaunchers: {   ChromeDebugging: {     base: 'Chrome',     flags: [ '--remote-debugging-port=9333' ]   } }, 

With such setup you can just run your karma server (with captured browser), and then start debugging in visual studio.

If you'd like to find more details I made a tutorial on debugging Karma with Visual Studio Code.

like image 192
Marek Lewandowski Avatar answered Sep 24 '22 06:09

Marek Lewandowski


Using Angular CLI 1.7.4: With the following steps I was able to debug a hello world Angular application with Visual Studio Code:

  1. Generate a fresh HelloWorld project:

    ng new HelloWorld

  2. Open the project in Visual Studio Code

    code HelloWorld

  3. Create a new Debug configuration:

    enter image description here enter image description here

  4. A .vscode/launch.json file is generated and opened. Replace its content by the following:

{     // Use IntelliSense to learn about possible attributes.     // Hover to view descriptions of existing attributes.     // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387     "version": "0.2.0",     "configurations": [         {             "type": "chrome",             "request": "launch",             "name": "Karma Tests",             "sourceMaps": true,             "webRoot": "${workspaceRoot}",             "url": "http://localhost:9876/debug.html",             // "runtimeArgs": [             //     "--headless"             // ],             "pathMapping": {                 "/": "${workspaceRoot}",                 "/base/": "${workspaceRoot}/"             },             "sourceMapPathOverrides": {                 "webpack:///./*": "${webRoot}/*",                 "webpack:///src/*": "${webRoot}/*",                 "webpack:///*": "*",                 "webpack:///./~/*": "${webRoot}/node_modules/*",                 "meteor://💻app/*": "${webRoot}/*"             }         }     ] } 
  1. Open karma.conf.js and perform the following change:

    enter image description here

  2. Open a terminal and start karma tests:

    ng test

  3. Open app.component.spec.ts and set a break point:

    enter image description here

  4. Select "Karma Tests" in the debug menu:

    enter image description here

  5. Press F5 to start debugging. VSCode should stop at the breakpoint:

    enter image description here

like image 21
Awsed Avatar answered Sep 24 '22 06:09

Awsed