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

Angular Unit Test: How to mock properties in a method?

Here is the service that I am trying to test: @Injectable() export class BomRevisiosnsService { constructor( private baseService: BaseService, private appConstants: AppConstants, private dmConstants: DMConstants ) {…
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
0
votes
1 answer

Error:- Can't read property variable of undefined during unit testing

variable from service is coming as undefined Below is my ts file ngOnInit() { this.serviceElectionForm.form = this.formBuilder.group({}); /**** * commenting below, if condition, doesnt cause any error in spec file …
developer
  • 301
  • 3
  • 14
0
votes
1 answer

How to Test router subscription

I am subscribing to route on ngOnInit. constructor(private route: ActivatedRoute) ngOnInit() { this.route.queryParamMap.subscribe(params => { this.mode = 'test'; }) } I was able to test all other methods in my component except the route…
kaounKaoun
  • 601
  • 1
  • 9
  • 21
0
votes
1 answer

Error as Cannot read property 'subscribe' . for activated route in angular 7 while unit testing

I am getting error as Cannot read property 'subscribe' of undefined for activated route in angular 7 while unit testing .I have tried it in multiple way kindly help . Observer.of is not working for me as i have rx.js version 6.3.3 here is my…
0
votes
1 answer

Unit test a subscription in ngOnInit not working

I have subsriber to an observable on ngOnInit and now I am trying to write test for this which doesn't seems working. Here is my class which I am trying to test ngOnInit() { this.myService.updates$.pipe(takeUntil(unsusbribe$)) .subscribe(update…
undefined
  • 3,464
  • 11
  • 48
  • 90
0
votes
1 answer

Observable unit test passing when it should be failing

New to rxjs and angular, attempting to write some basic unit tests for a simple rxjs observable with a success / error scenario: Service method: export class MyService { constructor(private http: HttpClient) {} public getId(id) { …
Oam Psy
  • 8,555
  • 32
  • 93
  • 157
0
votes
1 answer

code coverage not getting upload in Sonar Qube

I have coverage folder in Angular 4 project, i have to upload all ts code issue, code coverage to sonarQube. Except code coverage everything works fine. following is my…
Raj N
  • 249
  • 1
  • 18
0
votes
1 answer

Jasmine Global Variable initialization

I am writing Angular component unit testing using jasmine. We are having class with static variables which is initialized during loading time of application. Currently we are initializing those static variables in beforeAll of the unit test of…
Vignesh Pandi
  • 349
  • 1
  • 4
  • 15
0
votes
0 answers

Angular spy On not working for stubbing mock

I have angular 4 application and i am writing test cases using jasmin + Karma My first test case working properly as it creates component and service instance, But second test cases is failing as it is not able to fetch mock data for 'getorders'…
Raj N
  • 249
  • 1
  • 18
0
votes
0 answers

Getting error while unit testing in angular:-"TypeError: Illegal constructor at new SignalrWindow "

I have used signalR in angular 4 project. While performing unit test getting this error TypeError: Illegal constructor at new Signalr Window Below is the service code which is injected in component. When I am trying to inject the same service in…
Param
  • 9
  • 4
0
votes
1 answer

ngrx router-store - testing router navigation effects

I have effects and tests working as follow: LoginRedirect Effect @Effect() public loginSuccess$: Observable = this.actions$.pipe( ofType(AuthActionTypes.LoginSuccess), map( action => action.payload ), concatMap(…
Ian Jamieson
  • 4,376
  • 2
  • 35
  • 55
0
votes
1 answer

environment.ts not working in testing Angular 2

Trying to write simple test in Angular 2 but getting error for environment.ts as below ERROR in ./web/environments/environment.ts Module build failed: Error: web\environments\environment.ts is missing from the TypeScript compilation. Please…
0
votes
1 answer

How to test app/root component in Angular 6

Recently I have 2 failed test cases related to the app component. When I use fixture.detectChanges() the test case failed with an error "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value:…
Terence
  • 652
  • 11
  • 34
0
votes
1 answer

Is it good to make real router navigation during angular test or fakeAsync?

While writing angular unit test for routing, is it okay to navigate with real values or we should use fakeAsync for router navigations? Option 1 it('navigate to "home" takes you to /home', fakeAsync(() => { router.navigate(['home']); …
JPS
  • 2,730
  • 5
  • 32
  • 54
0
votes
1 answer

Angular unit testing getting failed

I am trying to unit test the function editDependentInfo(), but I am getting error, because in that function, another function populateEditDepInfo() is being called and populateEditDepInfo have this.dependentsInfo property and test from…