The level of logging by karma can be changed in the karma.conf.js file.

Go to karma.conf.js and add/change the property logLevel to config.LOG_DISABLE if you don't want to see any logs or config.LOG_ERROR if you want to only see the errors. Like this:

module.exports = function (config) {
  config.set({
    ...,
    logLevel: config.LOG_ERROR,
    ...,
  })
}

If you are running karma from the command line you can append --log-level debug to the command for the same effect.

Answer from Tom on Stack Overflow
🌐
GitHub
github.com › angular › angular-cli › issues › 9757
[Documentation Request] ng test --log-level options · Issue #9757 · angular/angular-cli
February 23, 2018 - What would like to see implemented + what did you expect to see: The values available for the --log-level option
Author   DinoSourcesRex
🌐
npm
npmjs.com › package › @ng-lv › logging
@ng-lv/logging - npm
LogManager : log.logmanager registered Logger : log.map set log level : log.logmanager.setCurrentLogLevel(4, ['c1','c2']) export enum LogLevel { Nothing, // 0 Verbose, // 1 Debug, // 2 Info, // 3 Warning, // 4 Error, // 5 Critical // 6 } Run ng serve for a dev server.
      » npm install @ng-lv/logging
    
Published   May 12, 2017
Version   1.0.2
Author   LVM
🌐
GitHub
github.com › angular › angular-cli › issues › 1246
ng test logging · Issue #1246 · angular/angular-cli
June 29, 2016 - ng test --watch=false --build=false > log.txt · but it only write True in it. It would be good to have full output in a file like · ng test --watch=false --build=false --output=path\log.txt · Reactions are currently unavailable · No one assigned · P5The team acknowledges the request but does not plan to address it, it remains open for discussionThe team acknowledges the request but does not plan to address it, it remains open for discussion ·
Author   kamran-pervaiz
Top answer
1 of 6
71

Update for Angular version 9

The source files have been moved but you can still debug this way if you do the following steps

  • In devtools, select the sources tab
  • Press CTRL + P
  • Type in the name of the file you want to debug

Valid for versions below 9

The other answers are completely valid answers but having been using Angular for around 18 months now I tend to do it in the browser - chrome tools!

Run ng test then f12 and find the spec file via the webpack context. Add a breakpoint(s) and refresh and it will hit said breakpoints. As per screenshot

2 of 6
47

This is what worked for me with:

  • Angular 9.0.6 + Visual Studio Code 1.43.2
  • Angular 8.2.13 + Visual Studio Code 1.39.2
  • Angular 7, Angular CLI 1.0.* and Chrome on Windows 7.

Change configuration files

In your project root directory open karma.conf.js. Right after singleRun: false add , followed by this section:

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

Add configuration to .vscode/launch.json.

  • For versions 8.* - 9.* (note "pathMapping section!):

    {
      "type": "chrome",
      "request": "attach",
      "name": "Unit tests",
      "address": "localhost",
      "port": 9333,
      "sourceMaps": true,
      "webRoot": "${workspaceFolder}",
      "pathMapping": {
        "/_karma_webpack_": "${workspaceFolder}"
      }
    },
    
  • For version 7.*:

    {
      "type": "chrome",
      "request": "attach",
      "name": "Unit tests",
      "address": "localhost",
      "port": 9333,
      "sourceMaps": true,
      "webRoot": "${workspaceFolder}"
    },
    

Start debugging

  1. Run ng test --browsers ChromeDebug

  2. Wait for Chrome browser to start. You will see something like this in command line:

    01 06 2017 16:07:29.276:INFO [launcher]: Launching browser ChromeDebug with unlimited concurrency
    
  3. Set the breakpoint in one of your .spec.ts files.

  4. In Visual Studio Code choose Unit tests debug configuration and hit F5 ("Start Debugging" button).

  5. Press Shift+Ctrl+F5 or refresh the Chrome window to rerun the tests and hit the breakpoint.


For convenience

You can also modify your package.json and add a new script:

"test-debug": "ng test --browsers ChromeDebug",

Then next time you want to start ng test with debugging just run:

npm run test-debug

References:

  • Debugging Jasmine Unit tests running with Karma runner in VS Code
  • Debugging Karma tests with VSCode
  • Angular CLI 8.1.3 Debug Unit Tests configuration - Unverified breakpoint
  • microsoft/vscode-recipes - Chrome Debugging with Angular CLI
🌐
Testing-angular
testing-angular.com › debugging-tests
Debugging tests – Testing Angular
February 17, 2021 - Write debug output to the console using console.log, console.debug and friends.
🌐
Ng-log
ng-log.github.io › ng-log › 0.8.0 › logging
Logging - ng-log
Verbose level condition macros VLOG_IF, VLOG_EVERY_N and VLOG_IF_EVERY_N behave analogous to LOG_IF, LOG_EVERY_N, LOG_IF_EVERY_N, but accept a numeric verbosity level as opposed to a severity level. VLOG_IF(1, (size > 1024)) << "I’m printed when size is more than 1024 and when you run the " "program with --v=1 or more"; VLOG_EVERY_N(1, 10) << "I’m printed every 10th occurrence, and when you run the program " "with --v=1 or more. Present occurrence is " << nglog::COUNTER; VLOG_IF_EVERY_N(1, (size > 1024), 10) << "I’m printed on every 10th occurrence of case when size is more " " than 1024, when you run the program with --v=1 or more.
🌐
Contiki-ng
docs.contiki-ng.org › en › develop › doc › getting-started › The-Contiki-NG-logging-system.html
The Contiki‐NG logging system — Contiki-NG documentation
To enable any level at run-time, compile Contiki-NG with the maximum log level for all modules (this will however result in a larger ROM usage, and may not be practical on some platform). To support per-module logging, .c files that implement must set the following: #include "sys/log.h" #define LOG_MODULE "Test" #define LOG_LEVEL LOG_LEVEL_INFO
🌐
DEV Community
dev.to › alisaduncan › how-to-level-up-your-angular-unit-testing-game-13-7a1
How To Level Up Your Angular Unit Testing Game (1/3) - DEV Community
January 21, 2019 - How can we figure out why a test is failing? Can we debug unit tests? Why yes, we can. Depending on whether you are using Angular v6 or Angular v7, the instructions are different. In Angular v7, after starting ng test, you can debug an individual test by following the steps below:
Find elsewhere
🌐
npm
npmjs.com › package › ng-logger
ng-logger - npm
January 4, 2017 - import { Component, OnInit } from '@angular/core'; import { LoggerService } from 'ng-logger'; import { environment } from '../environments/environment'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'MyApp'; constructor( private log: LoggerService) { } ngOnInit() { // set the configuration directly this.log.logLevel = 'DEBUG'; this.log. // ..OR import the log level from your environment config (ng-cli for example) if (environment.logger) { this.log.logLevel = environment.logger
      » npm install ng-logger
    
Published   Jan 04, 2017
Version   1.1.0
Author   Ralph Capasso
🌐
GitHub
github.com › angular › angular-cli › issues › 2582
Angular CLI is up and running event ng test --watch false after finishing · Issue #2582 · angular/angular-cli
October 8, 2016 - $ ng test --browsers PhantomJS --watch false --log-level debug is successful for the first time, but 2nd invocation causes warnings: WARNING in ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js 45:15 Critical dependency: the request of a dependency is an expression
Author   pranasblk
🌐
npm
npmjs.com › package › ngx-logger
ngx-logger - npm
April 13, 2023 - import { Component } from "@angular/core"; import { NGXLogger } from "ngx-logger"; @Component({ selector: "your-component", templateUrl: "./your.component.html", styleUrls: ["your.component.scss"], }) export class YourComponent { constructor(private logger: NGXLogger) { this.logger.error("Your log message goes here"); this.logger.warn("Multiple", "Argument", "support"); } } For most browsers, you need to enable "verbose" or "debug" mode in the developper tools to see debug logs · Configuration is sent by the forRoot call LoggerModule.forRoot({level: NgxLoggerLevel.DEBUG})
      » npm install ngx-logger
    
Published   Apr 13, 2023
Version   5.0.12
🌐
Jvandemo
jvandemo.com › how-to-access-angular-log-debug-messages-from-within-karma
How to access Angular $log debug messages from within Karma
August 24, 2022 - When running unit tests with Karma however, you will find that your debug logs don't appear in Karma's log output: ... PhantomJS 1.9.8 (Mac OS X): Executed 29 of 54 SUCCESS (0 secs / 0.004 secs) PhantomJS 1.9.8 (Mac OS X): Executed 30 of 54 SUCCESS (0 secs / 0.004 secs) ... Enabling the debug log level in your karma config:
🌐
CodeMag
codemag.com › article › 1711021 › Logging-in-Angular-Applications
Angular: Adding Logging in Angular Applications
October 27, 2017 - In this article, you'll build up the logging service in a series of steps. First, you create a simple log service class to log messages using console.log(). Next, you add some logging levels so you can report on debug, warning, error, and other types of log messages.
🌐
TutorialsPoint
tutorialspoint.com › testng › testng_custom_logger.htm
TestNG - Custom Logging
testMethodOne--Test method success testMethodTwo--Test method failed testMethodThree--Test method skipped =============================================== Simple Logger Suite Total tests run: 3, Failures: 1, Skips: 1 ===============================================
🌐
GitHub
github.com › ng-log › ng-log
GitHub - ng-log/ng-log: C++ library for application-level logging · GitHub
ng-log is a C++14 library that implements application-level logging.
Starred by 101 users
Forked by 17 users
Languages   C++ 88.0% | CMake 8.7% | Starlark 1.7% | Shell 1.6%
🌐
Automation Panda
automationpanda.com › 2018 › 01 › 15 › missing-error-messages-with-angular-testing
Missing Error Messages with Angular Testing | Automation Panda
January 18, 2018 - I recently had a devil of a time figuring out why my Angular unit tests were broken because error messages were not being printed! Running "ng test --sourcemaps=false" solved the problem.
🌐
GitAnswer
gitanswer.com › angular-cli-ng-test-log-level-options-typescript-299865494
ng test --log-level options | GitAnswer
What would like to see implemented + what did you expect to see: The values available for the --log-level option ... ng test --log-level none Problem with log4js configuration: ({ appenders: { console: { type: 'console', layout: { type: 'pattern', pattern: '%[%d{DATE}:%p [%c]: %]%m' } } }, categories: { default: { appenders: [ 'console' ], level: 'none' } } }) - category "default" is not valid (level "none" not recognised; valid levels are ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, MARK, OFF)