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

how can i test angular acrivated route subscribe together with snapshot data?

I have code that i subscribe to activatedRoute.url and inside the subscription im accsessing the sanpshot data. I created mock for the activatedRoute but my test keeps failing . This is the test code This is the code i want to test This is the…
0
votes
1 answer

Angular Unit Testing: How to unit test subscription which get value from function?

I have one subscription inside my ngoninit in ts file. I want to unit test this subscription how should i can do this? code.ts this.generalConfigSubscription = this.generalConfigService.getGeneralConfigState().subscribe((response) => { if…
0
votes
0 answers

Angular unit test Cannot read properties of undefined (reading 'find')

I'm facing a problem when i try to test object from array if it's visibility is false I can't reach the object Typescript code this.currentActionEvent = event; switch (event) { case imp.quickAction.afterNewQuick: …
Omar Khaled
  • 77
  • 2
  • 10
0
votes
1 answer

Unit-Test for a switch statement on Angular

I'm new to angular unit testing and I don't know how to test this switch: displayBadge(text: any) { switch (text) { case 'blabla': return 'badge badge-pill badge-success'; case 'lalala': return 'badge badge-pill…
0
votes
1 answer

Angular Karma Jasmine Unit Test An error was thrown in afterAll TypeError: users.forEach is not a function

I am unit test case for below code. I have mention component and component.ts.spec file. Component Code. this.commonService.postdata('users', request).subscribe((users: any[]) => { users.forEach((data) => { …
Swapnil Yeole
  • 406
  • 3
  • 12
  • 26
0
votes
1 answer

Angular Unit Test: How to create component instance if constructor takes Globals.ts file as an argument

I am trying to write a unit test for the Login Component of my angular 6 application. The login.component.ts file has the following constructor constructor(public global: Globals, private appService: AppService ) { } Here Globals is a normal ts…
LearnToCode
  • 169
  • 1
  • 5
  • 19
0
votes
0 answers

Angular unit test verifying childcomponent's method call

I have a page with sidebar section with sidebar-component and content section with a page-component. From the page-component, when I click a button, the sidebar-component toggles. Below is the code, async onSidebarButtonPressed() { const…
Raajkumar
  • 857
  • 2
  • 13
  • 26
0
votes
0 answers

PrimeNg's ConfirmationService can't be injected in Angular Unittest

I'm trying to write Angular (version 12.2.0) unittests the first time and there occured a problem I can't solve. The following code ist my unittest: import { ConfirmDialogModule } from 'primeng/confirmdialog'; import { ConfirmationService,…
Lisa Em
  • 1
  • 2
0
votes
1 answer

Angular, Rxjs, catch exception thrown in the exception handling code

In this pseudo code example this.someService.someMethod().subscribe( (data) => { throw Exception } (err) => { throw Exception } How do I catch an error that is thrown inside the subscribe methods? My specific scenario is…
tony
  • 2,178
  • 2
  • 23
  • 40
0
votes
1 answer

Why I cant call a component's function in .spec.ts?

I'm trying to unit test a function that calls another function. This is the called function detailsFiltersClosed(recentSearch: boolean): void { this.arrowUp = false; this.filterData = this.loadsFilter.getData(); const…
Julio Rodríguez
  • 447
  • 1
  • 8
  • 23
0
votes
1 answer

Angular test .spec.ts How to select multiple options

I have a multiple Select list that I want to test, basically when you select n items you then click a button that will copy the items from one list an another. I am having problems trying to select more than one value. This is a multi select so I…
Andrew
  • 2,571
  • 2
  • 31
  • 56
0
votes
1 answer

unit testing angular directive throws Access to XMLHttpRequest ...error

This is my directive @Directive({ selector: '[appTitleCase]', }) export class TitleCaseDirective { @HostListener('change', ['$event']) onChange($event: Event) { const titleCaseValue = TextHelpers.convertToTitleCase( ($event.target as…
nshathish
  • 417
  • 8
  • 23
0
votes
1 answer

How to mock a service in Angular unit testing?

I have the following service subscription which is promise getTaskData = async() { line 1 let response = this.getTaskSV.getTaskData().toPromise(); line 2 this.loading = false; } I tried like below, it('should load getTaskData', async () =>…
0
votes
0 answers

Angular typescript TypeError: Cannot read property 'setDomLayout' of undefined - test component with ag-grid

I have a component using ag-grid. @Component({ selector: 'app-summary', templateUrl: './summary.component.html', styleUrls: ['./summary.component.scss'] }) export class SummaryComponent implements OnInit { IsChecked: boolean = false; …
Mihir
  • 531
  • 2
  • 10
  • 35
0
votes
0 answers

angular unit test for throwing error fails

Hi I wrote an angular unit test for a method which throws an error when there is no data. But that test is failing with the message TypeError: Cannot read property 'subscribe' of undefined. I tried so many things but still it fails. This is the…
Nirdha
  • 95
  • 1
  • 1
  • 9