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.
Answer from Marek Lewandowski on Stack OverflowYou 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.
Using Angular CLI 1.7.4: With the following steps I was able to debug a hello world Angular application with Visual Studio Code:
Generate a fresh HelloWorld project:
ng new HelloWorldOpen the project in Visual Studio Code
code HelloWorldCreate a new Debug configuration:

A
.vscode/launch.jsonfile 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}/*"
}
}
]
}
Open
karma.conf.jsand perform the following change:
Open a terminal and start karma tests:
ng testOpen
app.component.spec.tsand set a break point:
Select "Karma Tests" in the debug menu:

Press
F5to start debugging. VSCode should stop at the breakpoint:
In
karma.conf.js:browsers = ['Chrome'];In your failing spec:
it('spec', function() { debugger; // This is like setting a breakpoint // ... });- Run Karma.
- Go to the newly opened Chrome Browser, open the console and refresh the page.
Now in Chrome's Developer Tools source tab you should see the execution stopped at the debugger.
Include "browsers = ['Chrome'];" in your karma.config file.
When Chrome opens, you should see "Karma - connected" at the top, with a "Debug" button to the upper-right.
Click this debug button, and a "Karma DEBUG RUNNER" tab will open. Then, simply right-click, inspect element, and debug as you normally would.


