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

Answer from 72GM on Stack Overflow
๐ŸŒ
Testing-angular
testing-angular.com โ€บ debugging-tests
Debugging tests โ€“ Testing Angular
February 17, 2021 - describe('Example spec', () => { fit('one spec', () => { /* โ€ฆ */ }); it('another spec', () => { /* โ€ฆ */ }); }); This improves the developing experience tremendously. The Webpack module bundler still re-emits the whole bundle even if you ...
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
48

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
๐ŸŒ
Angular
angular.dev โ€บ guide โ€บ testing โ€บ debugging
Debugging tests โ€ข Angular
For example, you can use the built-in Node.js debugger in VS Code or the Chrome DevTools for Node.js. The same way you start a debugging session with in Node, you can use ng test with the --debug flag with Vitest and browser mode.
๐ŸŒ
Angular
angular.dev โ€บ cli โ€บ test
ng test โ€ข Angular
... Specifies a regular expression pattern to match against test suite and test names. Only tests with a name matching the pattern will be executed. For example, ^App will run only tests in suites beginning with 'App'.
๐ŸŒ
Angular
v17.angular.io โ€บ guide โ€บ test-debugging
Angular
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
๐ŸŒ
YouTube
youtube.com โ€บ watch
๐Ÿ’ฅ How To Debug Angular Tests - A Step-by-Step Example of How To Troubleshoot a Failing Test - YouTube
This video is part of the Angular Advanced Library Laboratory: Build Your Own Library Course - https://angular-university.io/course/angular-advanced-course?...
Published ย  May 23, 2017
๐ŸŒ
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 - Run unit tests for Angular with the following command: ng test ยท When the tests start running you will see the Karma test runner launch in the Chrome browser window. There is also a Debug button shown in the top right corner. Once you have clicked on this you will be taken to the next screen, your tests will be in debug mode.
๐ŸŒ
Spark Code Hub
sparkcodehub.com โ€บ angular โ€บ testing โ€บ debug-unit-tests
Debugging Unit Tests in Angular: Techniques and Best Practices
... CI environments (e.g., GitHub Actions) often use headless browsers like ChromeHeadless, which may expose timing or environment issues. Run ยท ng test --browsers=ChromeHeadless locally to replicate CI conditions. Also, check for missing dependencies or network issues in CI.
Find elsewhere
๐ŸŒ
Angular
v17.angular.io โ€บ cli โ€บ test
ng test
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
๐ŸŒ
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 - We use "type": "pwa-chrome" instead ... need the Debugger for Chrome extension. However, if you have that extension installed, feel free to substitute "pwa-chrome" for "chrome" โ€” the rest of the config can remain the same. "restart": true ensures that the task keeps running even when our tests exit โ€” useful if you are testing files one at a time, for example. Run your Angular tests however you like โ€” either via ng test in the ...
๐ŸŒ
Angular
angular.dev โ€บ guide โ€บ testing โ€บ components-basics
Basics of testing components โ€ข Angular
The CLI creates an initial test file for you by default when you ask it to generate a new component. For example, the following CLI command generates a Banner component in the app/banner folder (with inline template and styles): ng generate component banner --inline-template --inline-style
๐ŸŒ
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:
๐ŸŒ
Studiolacosanostra
studiolacosanostra.github.io โ€บ 2019 โ€บ 05 โ€บ 16 โ€บ How-to-debug-Angular-tests-in-VSCode
How to debug Angular tests in VSCode
April 14, 2024 - And when you need to check what is happening in a given test, all you have to do is insert a breakpoint on a given line or add debugger, press F5 and you have everything you need.
๐ŸŒ
Testing-angular
testing-angular.com โ€บ testing-components
Testing Components โ€“ Testing Angular
What is wrong here? Did we forget to call detectChanges again? No, but we forgot to call ngOnChanges!
๐ŸŒ
Angular
angular.dev โ€บ guide โ€บ testing โ€บ components-scenarios
Component testing scenarios โ€ข Angular
The immediate goal is to test the DashboardHero component, not the Dashboard component, so, try the second and third options. Here's the meat of the spec file setup. let fixture: ComponentFixture<DashboardHero>; let comp: DashboardHero; let heroDe: DebugElement; let heroEl: HTMLElement; let expectedHero: Hero; beforeEach(async () => { fixture = TestBed.createComponent(DashboardHero); comp = fixture.componentInstance; // find the hero's DebugElement and element heroDe = fixture.debugElement.query(By.css('.hero')); heroEl = heroDe.nativeElement; // mock the hero supplied by the parent component expectedHero = {id: 42, name: 'Test Name'}; // simulate the parent setting the input property with that hero fixture.componentRef.setInput('hero', expectedHero); // wait for initial data binding await fixture.whenStable(); });
๐ŸŒ
GitHub
github.com โ€บ angular โ€บ angular-cli โ€บ issues โ€บ 27117
How to debug jest tests? ยท Issue #27117 ยท angular/angular-cli
February 16, 2024 - Can be a full path for a file, relative to the current workspace or module specifier. Example: 'zone.js'. [array] --ts-config The name of the TypeScript configuration file. See also related issue: #25434. In my comment I suggest a way to run ng build --test (or ng test --build).
Author ย  nicojs
๐ŸŒ
Angular
angular.dev โ€บ guide โ€บ testing
Testing โ€ข Overview โ€ข Angular
To test your Angular application in a CI server, run the standard test command: ... Most CI servers set a CI=true environment variable, which ng test detects.