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

Test value inside finalize pipe in angular test

I have the following scenario: app.component.ts isLoading = false; someFunction(){ this.isLoading = true; this.someService .someFuncInService .pipe(finalize(() => (this.isLoading = false))) .subscribe(data => { ... …
2
votes
1 answer

Spectator: override provider for single test (angular universal)

I built a small angular app and now I'm writing unit tests. So far so good, but when I try to test my authGuard I ran into some problems. I am using Spectator. I provide the platformId in the providers section in my spec, but I want to be able to…
Chai
  • 1,796
  • 2
  • 18
  • 31
2
votes
0 answers

How to test catchError scenario in Angular 8 jasmine so that handleError method will be covered

Method in service createAddress(reqBody): Observable { return this.http .post(`APIUrl`, reqBody) .pipe(catchError(this.handleError)); } handleError method:: handleError(error) { let…
2
votes
1 answer

TypeError: Cannot read property 'pipe' of undefined - Angular Unit test

I'm trying to write a unit test and I'm getting this error. TypeError: Cannot read property 'pipe' of undefined at Jasmine const mockServiceObj = jasmine.createSpyObj < FormRegistrationService > (["registerForm", "pipe"]); const…
Jacob
  • 59
  • 7
2
votes
1 answer

"Async function did not complete within 5000ms" when creating a unit test on a page with AGM

I just created a component with a map for my ionic+angular app. It's quite simple, just display one marker at one location: Spots
J4N
  • 19,480
  • 39
  • 187
  • 340
2
votes
1 answer

Unit testing - onGridReady - Ag-grid event in Angular

I am having a function like below in my component file like below onGridReady(params) { this.gridApi = params.api; this.columnApi = params.columnApi; this.gridApi.setDomLayout('autoHeight'); this.gridApi.sizeColumnsToFit(); …
2
votes
1 answer

Angular: Testing Http POST method

I have a simple service method that does an http post call to add a todoList: add(event: any): Observable { let todoListToAdd: TodoList = { name: event.target.value, listItems: []}; return…
xeraphim
  • 4,375
  • 9
  • 54
  • 102
2
votes
2 answers

Angular Testing error - this._formBuilder.group is not a function

I have a standard function to register users. In the test file, I am getting the error: TypeError: this._formBuilder.group is not a function This seems to be from the ngOnInit function. I have tried importing the testing module numerous ways - as…
Joshua Foxworth
  • 1,236
  • 1
  • 22
  • 50
2
votes
2 answers

How to handle dependency injection into Angular unit test using Jasmine?

I am trying to implement unit test in Angular for the first time, I am using Jasmine. I came from Java\Spring and using Spring framework it automatically inject all the dependencies into my test but I have no idea about how to do it using Angular…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
2
votes
1 answer

Angular: Setting up starting value of FormArray for Unit Tests

I am trying to unit test this method I created that is raised when a checkbox in a series of checkbox is changed. We take the event passed, determine the value and if the checkbox was checked checked and then amend a Form Array value depending if…
2
votes
0 answers

Angular Unit Testing with Dynamic Component Failed with error TypeError: Cannot read property 'ɵcmp' of undefined

list of Angular dependency "@angular/common": "~9.1.1", "@angular/compiler": "~9.1.1", "@angular/core": "~9.1.1", "@angular/forms": "~9.1.1", "@angular/platform-browser": "~9.1.1", "@angular/platform-browser-dynamic":…
2
votes
1 answer

Angular Spectator setInput - how to pass object

Maybe it's the same bug like this question... In my spectator test I wanna test passing an input to a component: sidebar.component.ts: export class SidebarComponent { @Input() projects: Project[]; filteredProjects: Observable; …
2
votes
2 answers

Angular8 unit testing jasmine timeout issue

Running into random unit test failures with this failure mode Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. Some of these tests that failed do not even have async testing going…
2
votes
1 answer

Angular Unit test - how to mock a provider in component which was being called by ngrx/effect during store dispatch

I have a component, inside ngOninit, I am dispatching an action which is being handled by ngrx/effect to get some data from service API. I am writing unit tests and want to mock the service call in ngOninit so that the original API is not called.…
Niranjan
  • 77
  • 9
2
votes
1 answer

Angular 4 unit testing template parse errors:

I am getting the following error when I run angular unit test using Jasmine. Error: Template parse errors: Property binding cdkHeaderRowDef not used by any directive on an embedded template. Make sure that the property name is spelled correctly…
keshavmurthy
  • 37
  • 1
  • 11