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
5
votes
1 answer

how do I test an interceptor which calls an observable with Angular 6+?

I have built an HttpInterceptor that closely follows the one in the Angular Documentation. However, I needed to make an asynchronous call as part of my interceptor. I created a StackBlitz with a simplified version of the code (but identical in…
4
votes
0 answers

Getting error: cannot access "XComponent" before initialization in angular unit test

I have setting up a unit test for one of my component, the test code is : describe('CompanyDetails', () => { let fixture: ComponentFixture; let component: CompanyDetailsComponent; beforeEach(() => { …
4
votes
0 answers

How to get the value of a select when using ngValue in Angular unit tests

In my Angular app I have a
4
votes
1 answer

Angular: Mocking of HttpClient in Unit Test

I have a service which uses a httpClient that I want to test: @Injectable({ providedIn: 'root' }) export class TodolistService { constructor(private http: HttpClient) { } getTodoLists(): Observable> { return…
xeraphim
  • 4,375
  • 9
  • 54
  • 102
4
votes
0 answers

Azure devops - minimal code coverage pull reqeust

I have an Angular 9 project and I am trying to get the minimal code coverage working on an pull request in Azure devops conform the documentation. However the minimal code coverage isn't working, probably missing some step.... Steps to…
4
votes
1 answer

Test if class is changed with Jasmine and Karma

I am trying to test if a button was clicked by the following way: When the button is clicked some element class is changed from wrapper-container to wrapper-container-expanded(I don't want to check if the function is called when the button is…
MD10
  • 1,313
  • 3
  • 12
  • 35
4
votes
1 answer

ngrx/store - testing, throw TypeError: Cannot read property 'pipe' of undefined

Here is my ts file: this.store.pipe(select(subscribe.getRegCategories)).pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => { if (data && data.length) { this.allRegCategories = data; } }); when i go…
user2024080
  • 1
  • 14
  • 56
  • 96
4
votes
1 answer

(Angular Unit-Test) How to mock input property in Jasmin?

I am currently trying to mock input properties for an Angular unit test. Unfortunately, I can not get any further and repeatedly receive the following error message: TypeError: Cannot read property 'data' of undefined My HTML Template looks like…
Codehan25
  • 2,704
  • 10
  • 47
  • 94
4
votes
1 answer

failed to run unit test for component that extends a base class in angular

I am trying to write unit test for a component that extends a base class. component.ts export class DesktopPresentationAreaComponent extends AbstractPresentationComponent { constructor() { super(store, webcast); …
Pranab V V
  • 1,386
  • 5
  • 27
  • 53
4
votes
1 answer

Angular 6 Unit Test ngOnInit with a setTimeOut not working

I have a component with a setTimeOut function inside a ngOnInit function. To write unit test cases for that I'm using tick and fakeAsync to fast forward the setTimeOut. But, it's not getting executed which in turn is not calling other function…
Varun
  • 3,695
  • 6
  • 21
  • 34
4
votes
2 answers

Angular 6: How to write jasmine test spec for a mat-dialog

I trying to write a test spec for mat-dialog, but i could not be success, the problem is that it is called by a function. How to do that? Thanks for your help. Here is my code closeDialogCancelButton() { if (this.editFormData.dirty) { let…
Enrique_Iglesias
  • 121
  • 1
  • 2
  • 11
3
votes
1 answer

Angualr unit test - getting an error as `Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?`

Here is my basic setting of the unit test: describe('ShellViewComponentAttributesComponent', () => { let fixture: ComponentFixture; let component: ShellSelectLanguageForEEComponent; let store:…
user2024080
  • 1
  • 14
  • 56
  • 96
3
votes
1 answer

How to unit test nested functions in angular using jasmine-karna

I am trying to test SetRole function which is calling GetRole function in the end, but I am unable to cover the line in code coverage. Please guide me related to testing cascaded function, nested function, function in the…
3
votes
2 answers

Firefox/Chrome was not killed in 2000 ms, sending SIGKILL

After running ng test in my local angular(v10) project, all the test cases are executing successfully and the browser is also getting launched and I am able to see the green dots but at the last moment browser is killed with the following warning in…
3
votes
3 answers

How to test http.post request with parameters in angular 9?

I am new at angular I have a service that calls HTTP post requests with some parameters. Now I want to test that service function in angular. My service part is import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable }…
1
2
3
17 18