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

Angular unit tests are failing randomly due to Async callback was not invoked with 5000ms(set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

I have written the following code with async unit testing but these are failing randomly not consistently. Can anyone help me what here wrong? The test case is: When the user pressed keyboard keys W (go to the previous doc) and S(go to next doc)…
Anshita Singh
  • 1,583
  • 1
  • 17
  • 40
1
vote
1 answer

How to access @Input property in angular unit test

I am writing a jest unit tests for a simple angular component which have @Input property. Here is my component import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-title', template: '

Prasad Parab
  • 437
  • 1
  • 7
  • 26

1
vote
1 answer

Angular: how to unit test changes to HostBinding property

I have a component that dynamically changes it's role attribute via HostBinding: export class TestComponent { @HostBinding('attr.role') get role() { return this.isListItem ? 'listitem' : null; }; @Input() isListItem:…
shrewdbeans
  • 11,971
  • 23
  • 69
  • 115
1
vote
0 answers

How can i mock/fake a method in a service that was inherited from another service and gives back an Observable?

I want to write a unit test for a method in a service. However the method is written in another service and it is used here. I would like to mock/fake said method. This is the service: export class UserService extends ProxyService { …
FarkasB
  • 11
  • 1
1
vote
1 answer

Why this Jasmine unit test always fail (testing an Angular component) also if the output returned by my method is correct?

first time for me trying debugging an Angular project with Jasmine. I have this PeopleListComponent component class: import { Component, OnInit, ElementRef, ViewChild } from '@angular/core'; import { EventService } from '../event.service'; import…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
1
vote
1 answer

Why am I obtaining this error on my Jasmine unit test?

I am trying to implement an Angular Unit Test using Jasmine for the first time following some examp,e and I am finding some problem. So I have this PeopleListComponent class implmenting the logic of a compontent: import { Component, OnInit,…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
1
vote
0 answers

Angular unit testing ,"You need to include some adapter that implements __karma__.start method!"

I am installing karma for my project,I did the following things but when I run 'npm run test-karma', then I got the following error, "You need to include some adapter that implements karma.start method!" Package.json, "jasmine": "^3.3.1", …
learner
  • 357
  • 1
  • 6
  • 16
1
vote
1 answer

How to make the Angular 2 unit tests for Meta tags from the browser-platform library?

I am writing a unit tests for Angular 2 component with Jasmine. I will like to test if my document metatags as been set to a specific value when my component is instantiated. My component: import { Component } from '@angular/core'; import { Meta }…
1
vote
1 answer

Suggest angular unit test for code coverage

async ngOnInit() { if (localStorage.getItem('Authorization') && this.formdataservice.get_account_user_id()) { try { let res: any = await this.formdataservice.get_preferences().toPromise(); const { data } = res.body; …
1
vote
1 answer

getting Error: Unexpected value 'undefined' declared by the module 'DynamicTestModule' using jest+spectator angular 9

I am using angular 9 and replaced the default unit testing of angular to spectator and jest after deep research, its fun and I have few tests that work fine, but suddenly I have this issue when trying to test some parent component, resulting ugly…
1
vote
1 answer

this.fb.group({ is not a function in angular?

I have the following code, ** component.ts, constructor(public cdr:ChangeDetectorRef, public fb:FormBuilder) { this.buildForm(); } buildForm(): void { this.feedbackForm = this.fb.group({ suggestedAScore: ['',…
learner
  • 357
  • 1
  • 6
  • 16
1
vote
2 answers

How to write an unit and/or e2e test for function that calls services

How would I approach writing a unit test for this function? When the form is submitted, it sends a POST request to get a login token. With the token response it then sends another request to get the specific user details. Here is the function: //…
1
vote
2 answers

How to unit test a method that removes duplicates from an array?

I have the following method that is removing duplicates from an array based on a given property: removeDuplicates(myArr, prop) { return myArr.filter((object, pos, arr) => { return arr.map(obj =>…
Codehan25
  • 2,704
  • 10
  • 47
  • 94
1
vote
1 answer

How to unit test valueChanges method on ngx-mat-select-search in a dropdown

I am trying to write a unit test for one of the dropdown with a search box at its top associated with it. The problem is that valueChanges is not getting fired and I am unable to see the filteredValues to put an assert on. I see the searchbox being…
a p
  • 1,121
  • 6
  • 26
  • 51
1
vote
1 answer

Angular 7 swUpdate breaks unit testing and cannot be mock / override for testing (PWA)

Here is my component for implementing the swUpdate : public thereIsUpdate: boolean; constructor(public swUpdate: SwUpdate) { this.checkForUpdates(); } checkForUpdates() { this.swUpdate.checkForUpdate().then(() => { …