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 Overflow
Top answer
1 of 10
73

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.

2 of 10
36

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:

  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:

  2. Open a terminal and start karma tests:

    ng test

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

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

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

Discussions

How to inspect tests with devtools?
Now that I have failing tests, how to I use devtools to inspect the tests with breakpoints? Is there a way to make Chrome headless pause so that I can press play in devtools to let it run (similar ... More on github.com
🌐 github.com
5
May 4, 2018
debugging - How to debug Unit Tests with Karma/Jasmine in Visual Studio Code? - Stack Overflow
Excellent - after struggling to get debugging in unit testing working as described in the Microsoft/vscode-recipes GitHub repo, this finally worked for me. 2019-03-19T19:58:06.723Z+00:00 ... I get: Cannot load browser "Chrome_with_debugging": it is not registered! More on stackoverflow.com
🌐 stackoverflow.com
google chrome devtools - Debugging Karma/Jasmine Tests with Angular 2 in WebStorm - Stack Overflow
I presume that launches a different ... with ng test), but what specifically happens there is being done by webstorm. So, yes, it's exactly related to Webstorm. There's possibly a setting in Webstorm to fix this, but I haven't found it. – Amy Blankenship Commented Dec 19, 2016 at 4:31 · there are no settings in WebStorm that affect this. 'Little bug button' just runs the currently selected run configuration in debugger... If it's a karma run configuration, ... More on stackoverflow.com
🌐 stackoverflow.com
node.js - Debugging karma-jasmine tests with node-inspector - Stack Overflow
Almost the same question as Debugging jasmine-node tests with node-inspector BUT does anyone know how to use node-inspector with karma? ... Then start the node-inspector and background the process (use fg to bring it back to the foreground and kill %1 to stop it): ... Then connect to the inspector from your local loopback. To start debugging, open the following URL in Chrome... More on stackoverflow.com
🌐 stackoverflow.com
April 27, 2017
🌐
Google Groups
groups.google.com › g › karma-users › c › OocaR-ZgHO8
How to debug karma-based unit tests in Chrome or Firefox
By default, Karma opens browsers with an empty profile, without any extensions. You can also put "debugger" statement into the code and the browser will jump into debugger (as long as the web inspector is open), at least in Chrome.
🌐
Gleb Bahmutov
glebbahmutov.com › blog › debugging-karma-unit-tests
Debugging Karma Unit Tests | Better world by better software
April 23, 2024 - We are looking for --debug command line argument (using process.argv array), and if found, set the list of preprocessorts to empty. If we rerun the karma again with new argument, and open the debug tab, we get our original source code · $ karma start karma.conf.js --browsers=Chrome --single-run=false --debug · You can also put breakpoints in the spec files and step through the individual tests
🌐
Damir's Corner
damirscorner.com › blog › posts › 20161018-DebuggingKarmaTestsInABrowser.html
Debugging Karma Tests in a Browser | Damir's Corner
October 18, 2016 - Instead you should click the Debug button at the top of the test runner's web page to open a new detached browser tab for debugging, which doesn't report the results back to the runner.
🌐
Karma-runner
karma-runner.github.io › 0.10 › intro › faq.html
Karma - Frequently Asked Questions
You can fix this by setting the ... Check out browsers for more information. Go to the captured browser and click the "DEBUG" button (or open http://localhost:9876/debug.html) and use the web inspector to see what's going on....
🌐
Mlewandowski
blog.mlewandowski.com › Debugging-Karma-tests-with-VSCode.html
Debugging Karma tests with VSCode | Mlewand blog
The whole trick is to make Chrome Debugger extension to listen to Chrome instance launched by Karma, using Chrome Debugging Protocol. This is very simple, you just need to make sure you’re using a correct port. First let’s set the port in Karma’s configuration.
🌐
Karma-runner
karma-runner.github.io › 6.4 › intro › troubleshooting.html
Karma - Troubleshooting
You can fix this by setting the ... Check out [browsers] for more information. Go to the captured browser and click the "DEBUG" button (or open http://localhost:9876/debug.html) and use the web inspector to see what's going on....
Find elsewhere
🌐
GitHub
github.com › karma-runner › karma-chrome-launcher › issues › 179
How to inspect tests with devtools? · Issue #179 · karma-runner/karma-chrome-launcher
May 4, 2018 - Now that I have failing tests, how to I use devtools to inspect the tests with breakpoints? Is there a way to make Chrome headless pause so that I can press play in devtools to let it run (similar to --inspect-brk for Node apps)? I have · browsers: ['MyChromeHeadless'], customLaunchers: { MyChromeHeadless: { base: 'ChromeHeadless', flags: [ '--remote-debugging-port=9222' ], }, }, in my karma config, and I can manage to load that page, but the tests end quickly before I have time to do anything.
Author   trusktr
🌐
Andrewhalil
andrewhalil.com › 2021 › 05 › 18 › debugging-angular-unit-tests-with-the-karma-test-runner
How to Debug Angular Unit Tests with the Karma Test Runner – andrewhalil.com
May 18, 2021 - Once you have clicked on this you will be taken to the next screen, your tests will be in debug mode. Now open the Chrome Development Tools. Select the Sources tab. When presented with the source folder, locate the _karma_webpack_ folder, then expand the folders until you locate the test source ...
🌐
Medium
medium.com › @oswain_59572 › debugging-intermittent-test-failures-with-karma-and-jasmine-91e870e0c4f3
Debugging Intermittent Test Failures with Karma and Jasmine | by Oliver Carlson | Medium
June 13, 2023 - Double check that the tests are actually failing intermittently by testing them in isolation with `fit` and `fdescribe`. Turn off random execution to try to get some tests failing consistently. After you have fixed any test failures that show up with sequential test execution, start getting some seed values to fail consistently. Use your tools! Conditional breakpoints and line-by-line debugging with Chrome Developer tools are your friend.
🌐
Swaminathan Vetri
swaminathanvetri.in › 2017 › 04 › 07 › debugging-jasmine-unit-tests-running-with-karma-runner-in-vs-code
Debugging Jasmine Unit tests running with Karma runner in VS Code – Swaminathan Vetri
April 7, 2017 - To see the actual debugging in action, Click on Debug icon, and then select the Debug tests in Chrome configuration and click on the little green play button. VS Code should be able to attach the debugger to the Chrome instance launched by karma ...
🌐
Testing-angular
testing-angular.com › debugging-tests
Debugging tests – Testing Angular
February 17, 2021 - 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. The debug test runner does not have an iframe, it loads Jasmine directly.
🌐
Medium
medium.com › nextfaze › debug-angular-10-karma-tests-in-vscode-9685b0565e8
Debug Angular 12 Karma Tests in VSCode | by Zak Barbuto | NextFaze | Medium
October 4, 2021 - "restart": true ensures that the ... Run your Angular tests however you like — either via ng test in the terminal and then run the “Attach to Karma” configuration ......
🌐
JetBrains
youtrack.jetbrains.com › issue › WEB-31590
Debugging Karma tests in Chrome Headless : WEB-31590
November 17, 2022 - {{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
JetBrains
jetbrains.com › help › idea › run-debug-configuration-karma.html
Run/Debug Configuration: Karma | IntelliJ IDEA Documentation
March 8, 2026 - In this dialog, create configurations for running and debugging JavaScript unit tests using the Karma test runner.
🌐
JetBrains
jetbrains.com › help › idea › running-unit-tests-on-karma.html
Karma | IntelliJ IDEA Documentation
March 10, 2026 - Optionally, specify the command-line ... file. For example, to run or debug tests in Headless Chrome, type --browsers ChromeHeadless in the Karma options field....