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

Creating Mock for Sort Header Event Angular Material

I am trying to write a unit test for a matSortChange function. This emits a Sort Event that I thought was just an object with an active property and direction property. My test currently looks like: it('sort unit test', () => { const fakeSortEvent…
Brian Stanley
  • 2,078
  • 5
  • 24
  • 43
0
votes
0 answers

How to resolve issue that is res.headers.get('Authorization') is undefined in Angular unit testing?

Component function: onSubmit() { this.formDataService.sendUserCredentials(this.loginForm.get('email').value, this.loginForm.get('password').value) .subscribe(res => { localStorage.setItem('X-Auth-Token',…
0
votes
0 answers

Angular directive unit testing

I am having trouble with creating unit test for an Angular directive. The problem is that after setting inputEl.value and call input.dispatchEvent(new Event('input')) then fixture.detectChanges(), input value is deleted and will be an empty string…
0
votes
1 answer

Why branches coverage is random?

I have theses 4 unit tests. const handleHttp = (method: 'POST' | 'GET' | 'PUT' | 'DELETE'): void => { const req = httpTestingController.expectOne(`${environment.apiUrl}test`); expect(req.request.method).toEqual(method); return req.flush({…
Pierre D
  • 327
  • 1
  • 2
  • 7
0
votes
1 answer

UT not pass due to undefined FormArray control while the control must be defined

I have a workshop-edit component that (in order): Build the form Retrieve the workshop to edit Update form values with workshop values Here is the code: ngOnInit() { this.buildForms(); this.initialize(); } async initialize(): Promise { …
PierreD
  • 860
  • 1
  • 14
  • 30
0
votes
1 answer

How to unit test if something is !== using jasmine and check flag?

Hey so I have this method in my Angular app which I want to unit test: public methodEquip(someBonus: Parameters) { let flag = false; for (const shield of someBonus.items) { if (shield.added.length !== 0 || shield.removed.length !==…
0
votes
2 answers

watching for variable changes during unitt esting

I am working on a component that mainly does its job on onNgInit() method: stage = ''; onNgInit(){ const thus : any = this; this.stage = "start'; this.uniService.dataExists().then(result=>{ // the data exists.…
Wede Asmera Tseada
  • 513
  • 2
  • 4
  • 14
0
votes
1 answer

No component factory found for LoaderDialog

**testservice.ts** import { LoaderDialog } from './loader.component'; import { MatDialog } from '@angular/material/dialog'; export class TestService { constructor(public dialog: MatDialog) {} dialogRef:any; show() { …
Sathisha
  • 21
  • 2
0
votes
1 answer

Getting error while unit testing of angular component method?

I am running a unit test which is failing. Please look at my code: This is my typescript file: allData: any; constructor(@Inject(MAT_DIALOG_DATA) private data, private dialogRef: MatDialogRef, private _service:…
Dungeon
  • 972
  • 2
  • 16
  • 32
0
votes
1 answer

Jasmine - “Cannot read property 'values' of undefined thrown”

I just started writing unit tests for an existing Angular app using Jasmine, and around 50% of the time, I get the following error.The other 50% of the time, all tests pass without any issues. The major problem I have with this error message is that…
Sai Kumar
  • 31
  • 2
  • 12
0
votes
1 answer

Angular Unit testing with Input decorator throw error

here is my component which i run testing: import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'shell-bread-crumbs', templateUrl: './shell-bread-crumbs.component.html', styleUrls:…
user2024080
  • 1
  • 14
  • 56
  • 96
0
votes
0 answers

how to resolve the error which is " 'includes' undefined in angular unit test"

I am writing unit test case where I'm facing jasmine error This is for angular application not sure the reason why this is happening Error Uncaught Error: Uncaught (in promise): TypeError: Cannot read property 'includes' of undefined …
0
votes
1 answer

Angular, unit testing a component that creates a service via an interface

Given this component that creates a service locally @Component({ providers: [ { provide: 'IMyService', useClass: MyService }, ] }) export class MyComponent implements OnInit, OnDestroy, AfterViewInit { …
tony
  • 2,178
  • 2
  • 23
  • 40
0
votes
1 answer

Type assertion using the '<>' is forbidden,use 'as' instead?

I have the following test case in my spec, it (should create,()=>{ const url = ( http.get).calls.args(0)[0] expect(url).toBe('/api/get') }); When I run it, I am getting the following lint error. Type assertion using the…
learner
  • 357
  • 1
  • 6
  • 16
0
votes
1 answer

How to write the test cases in angular if there is a init.spec.ts file present?

I have a test folder in which there are 3 files, 1)nav-bar.init.spec.ts 2)nav-bar.scaffold.ts 3)nav-bar.spec.ts I have the following codes respectively, In nav-bar.init.spec.ts, import { TestBed, async } from '@angular/core/testing'; …
learner
  • 357
  • 1
  • 6
  • 16