Questions tagged [angular-unit-test]

Questions about Angular unit testing. Use this tag for Angular unit testing questions which are not specific to an individual version. Do not use this tag for the older AngularJS (1.x) web framework uni testing questions.

Angular unit testing is based on two systems. Jasmine (The testing framework) and Karma (The test runner).

The Angular CLI takes care of Jasmine and karma configuration for you.

You can fine-tune many options by editing the karma.conf.js and the test.ts files in the src/ folder.

The projects that you create with the CLI are immediately ready to test. Just run ng test CLI command to run the tests.

Useful Links

256 questions
87
votes
4 answers

Angular CLI create .spec files for already existing components

There is an option in .angular-cli.json to disable the automatic creating of *.spec files e.g. for components, see json schema. This is a really nice feature because personally (this is just my opinion) testing the components might not be the thing…
smnbbrv
  • 23,502
  • 9
  • 78
  • 109
15
votes
3 answers

How to access nativeElement attribute using debugeElement in angular unit test

I want to write a simple unit test to check if button is disable when certain value is null or empty. Below is my spec file: test.spec.ts import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import 'reflect-metadata'; import {…
Always_a_learner
  • 4,585
  • 13
  • 63
  • 112
12
votes
2 answers

ngOnChanges not called in Angular 4 unit test detectChanges()

The question I have a button component that accepts a promise and disables the button until the promise has resolved and I want to write a unit test for this functionality. My Code My button component has an input for the promise /** * A single…
11
votes
1 answer

Angular Unit Test for passing FormGroup as @Input

I want to do a few unit tests there is a formGroup passing as input from parent component to a child component, how to do it. app.componet.ts import { Component, Input } from '@angular/core'; import { FormGroup, FormControl } from…
Zahidur Rahman
  • 1,688
  • 2
  • 20
  • 30
11
votes
2 answers

Angular 9 TestBed.inject & Provider Overrides

When using provider overrides what is the alternative of the following now that TestBed.get has been deprecated in Angular 9 TestBed.configureTestingModule({ providers: [{ provide: MyClass, useClass: MyStub}] }); const obj : MyStub =…
C. Rodwell
  • 113
  • 1
  • 4
11
votes
4 answers

"An error was thrown in afterAll\n[object ErrorEvent] thrown" - Angular 4 Unit tests

While I was working on Angular 4 unit tests, one of the pages which uses google maps and agm package showed an error: An error was thrown in afterAll\n[object ErrorEvent] thrown Does anyone know what the issue could be? Console Screenshot
Tetali pratyush
  • 121
  • 1
  • 1
  • 3
10
votes
2 answers

Angular Test Error - NullInjectorError: No provider for TrimInputDirective

I created an Angular Directive, that uses CSS selectors to automatically trim inputs in my application, it looks like so... import { Directive, HostListener, forwardRef } from '@angular/core'; import { DefaultValueAccessor, NG_VALUE_ACCESSOR } from…
9
votes
2 answers

How to Unit test the @viewChild ElementRef angular

My component file has the following code @ViewChild('clusterCard', { static: false }) clusterCard: ElementRef; highLightTheClusterCard(point: PickupClusterPoint) { if (point) { const card: HTMLElement = _get(this.clusterCard,…
sunil
  • 660
  • 8
  • 20
8
votes
6 answers

ng test fails in angular universal Error "Incomplete: No specs found, , randomized with seed 48751"

convert angular 7 project into angular universal while running "ng test" command giving error as "Incomplete: No specs found, , randomized with seed 48751". Tried different ways mention over stackoverflow but nothing work for me. ERROR in…
6
votes
4 answers

How to unit test window.location.reload in angular 12 unit testing?

In my angular project, I have a function reloadPage(): void { window.location.reload(); } So when ever I need to reload the page, I used to call this function. Now I am trying to add the unit testing for this function but it is not…
Arun
  • 3,640
  • 7
  • 44
  • 87
6
votes
1 answer

Unit testing of catcherror pipe function in angular service

I try to understand how to test this function. From (err)=>{ line, it's showing as an uncovered statement. service.ts Deletevote(inp) { console.log(inp); return this.http.post(environment.apiUrl + '/api/reset/abc', inp).pipe( …
user16443873
6
votes
1 answer

Mocking angular service class properties or variables

I am unit testing an angular application and there is a service I need to mock. I am able to mock service methods without any problem but when I try to mock properties in the same way it give me error My configuration service have one property and…
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
5
votes
1 answer

Unit Test - ag-grid API onFilterChanged of undefined in Angular 9

I am writing the unit test case for Ag-grid in Angular where I have Angular Grid: External Filter which is toggling filter checkbox. I'm getting "TypeError: Cannot read property 'onFilterChanged' of undefined" I'm testing this…
Bozhinovski
  • 2,496
  • 3
  • 20
  • 38
5
votes
2 answers

Angular 10 | ngrx Effects | Unit Tests | Cannot call a class as a function

After upgrading the Angular project to Angular 10, ngRx effects unit tests started breaking. Following is the error after upgrade: Running into the same issue, Follow the solution. Thanks,
Manish Kumar
  • 1,131
  • 15
  • 28
5
votes
1 answer

test angular if else block and subscribe(response => {} block in jasmine

I am facing an issue in writing test case on component method. How to test a Angular component method if else block inside subscribe((res) => { ///block} and ToasterService inside it. I have called the Angular service method in my component.ts…
Madhu
  • 75
  • 1
  • 2
  • 5
1
2 3
17 18