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
1
vote
2 answers

Can not access child nodes inside a DIV with Angular Unit Test

I have an Angular (using Angular 10) component with a HTML snippet like below:

Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line…

1
vote
0 answers

Cannot read property 'injector' of null

I was set up the angular project with the jest unit testing framework and after running the test case it's throwing me an error. TypeError: Cannot read property 'injector' of null app.component.spec.ts import { ComponentFixture, TestBed } from…
Rahil
  • 71
  • 2
  • 8
1
vote
1 answer

Unit test async method with multiple awaits

I am trying to test statements after 1st await but it doesn't work. Debugger doesn't hit the next breakpoint service.ts async deleteFaq(faq: FaqDetailsApi) { await this._http.delete(`${this.ADMIN_FAQ_URL}/${faq.id}`).toPromise(); debugger //…
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
1
vote
1 answer

How to get exact code coverage in sonarcloud for angular

I am trying to produce code coverage report in sonarcloud for angular-12. Could someone help me how to configure azure pipeline for generating correct code coverage percentage. Currently I am configurating as below, …
1
vote
1 answer

Check type of object returned from function in Angular 2 Unit Test Case

I am new to writing angular unit test. I have 2 methods in a component. Method 1 canEnableButton(isButtonEnabled: boolean): boolean { return isButtonEnabled || !this.isAvailable; } Method 2 getObjectValue(): CustomObject { if…
1
vote
1 answer

Angular Unit Test - What is the difference between new Service() and Testbed.inject()?

On Angular's website, we can see two ways to write unit test of Service. The first is to instantiate the service by new Service. let service: ValueService; beforeEach(() => { service = new ValueService(); }); Second, is instantiate the service by…
1
vote
0 answers

Unit Testing Nested Components UI elements in Angular using Jasmine and Karma

pseudo-code of my parentComponent html: I am creating an instance of child…
Ranjan V
  • 21
  • 4
1
vote
2 answers

Shouldn't fakeAsync prevent automatic subscription completion?

I thought that by using a fakeAsync wrapper, my tests wouldn't automatically run subscriptions and that I'd be controlling that part by calling tick manually, but that doesn't seem to be the case. For example, using this method: foo(): void { …
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
1
vote
1 answer

How to unit test and mock parentElement of ElementRef - Angular 9

I have a scroll-animation component which i can add to some other components in order to display a scroll animation. The scroll animation will be shown when the parent component has scroll-bar. export class ScrollIndicatorComponent implements…
Emil Kaminski
  • 1,886
  • 2
  • 16
  • 26
1
vote
2 answers

How to mock Rxjs Subject in Angular

I have a centralized DataStore to hold my report connections. So that I can manage report connections events like onShow, onError and onCancel; so the implementer doesn't have to. Anyways. How can I mock the SomeService.doSomething function so that…
Croeber
  • 451
  • 3
  • 10
  • 22
1
vote
1 answer

How to test resetForm function for template driven forms in angular using Jasmine Karm test?

I am trying to unit test resetform() function [used as TemplateVar.resetForm()] where loginVar is used as template variable for template driven form in angular. I am not getting any sources oridea ho to do that. Please guide. Thanks in…
1
vote
1 answer

Angular Unit Test - nested async pipes

Consider the following code: app.component.ts @Component({ selector: 'app-component', templateUrl: './app-component.html', styleUrls: ['./app-component.scss'] }) export class AppComponent implements OnInit { constructor( private…
userradu
  • 119
  • 2
  • 13
1
vote
1 answer

Stub file imports in Angular 2+ Unit Testing

My project has a lot of utility functions that perform some data transformations, etc. These functions are not in class and are reused in many utility files. I'm very new to angular testing. Searched for quite some time and I couldn't find a way to…
1
vote
0 answers

Angular8: Unit test pass multiple data to mat dialog

I have an angular app where I need to test a mat-dilaog by passing data. I am able to pass it through as below. { provide: MAT_DIALOG_DATA, useValue: staffData }, But I am trying to find a way to pass different values as part of staffData for…
indra257
  • 66
  • 3
  • 24
  • 50
1
vote
1 answer

How to reuse Mock dependencies in Angular Unit Test?

I'm writing unit test in Angular, I am mocking all our services in all the specs repeatly, the TestBed.configureTestingModule is having same code in all the spec files. is it possible to move this TestBed.configureTestingModule and Mock dependencies…
Cegone
  • 489
  • 1
  • 9
  • 23