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

Code coverage not happening while writing unit test case to the promise function

I am writing unit test cases by using angular 6 and jasmine. I am unable to cover the getUserJSON function after writing small test case on the below code function. How to cover hole function to get full percentage in code coverage for this…
sai
  • 67
  • 1
  • 10
1
vote
1 answer

How to test angular event listeners?

I have the following code, comp: ngOninit() { window.addEventListner('oreantationChange' () => { this.sideNavbar(); }); } Here I want to cover the above code and the following are my changes, spec: it ('-----',() => { …
learner
  • 357
  • 1
  • 6
  • 16
1
vote
0 answers

angular unit test:Cannot read property 'subscribe' of undefined issue

I am trying to write unit test for my code. component import {Component, Input, OnInit} from '@angular/core'; import {MainStorageService} from '@globals/storage/services/main-storage.service'; import {getSystemState, getWebcastState} from…
Pranab V V
  • 1,386
  • 5
  • 27
  • 53
1
vote
0 answers

How to check whether for assistance of element based on element name in angular unit testing?

I have a side navbar as follows, My ts, "navOptions" = [{'title':'profile'},{'title':'friends'}]; My spec, it('should render title in a h1…
learner
  • 357
  • 1
  • 6
  • 16
1
vote
1 answer

karma test cases are running multiple times: Angular unit test

I am not able to understand why my karma test cases re runs multiple times after a fixed test case. Disconnected, because no message in 50000 ms. Chrome 75.0.3770 (Windows 10.0.0): Executed 131 of 251 DISCONNECTED (1 min 9.028 secs / 18.285…
1
vote
1 answer

Testing angular app with large amount of injected services

I recently got an enterprise project to write unit tests for. The app is really huge. It has a large injection three. When I'm setting up the tests, it took me half day to mock only the injected services. The problem is, that the service I'm…
Runtime Terror
  • 6,242
  • 11
  • 50
  • 90
1
vote
1 answer

Unable to cover a service in constructor angular unit test

How can I solve this? Hello All, i am pretty new to unit test of angular 6 using karma and jasmine I am trying to cover a service which is inside my constructor of a component but unable to do it. The 'it' block 'should create component' covers…
Kishore Jv
  • 159
  • 5
  • 17
1
vote
1 answer

Angular unit test case failing while using component ngOnInit

I am using angular 7 and unable to run the unit test case while using component.ngOnInit() and fixture.detectchanges(). If remove component.ngOnInit() and fixture.detectchanges() then test case is passing but I want to detect changes after spy. …
sai
  • 67
  • 1
  • 10
1
vote
1 answer

How to verify ngCopy method call in angular 6 unit test

I am using ngCopy module for copying text to clipboard in one of my angular componet import {Component, Input, OnInit} from '@angular/core'; import {ngCopy} from 'angular-6-clipboard'; @Component({ selector: 'app-error-message', templateUrl:…
Prasanna
  • 33
  • 8
1
vote
1 answer

Calling function to clear localstorage value from unit test - angular

I want to test the if the localStorage is cleared down whenever I call my function. Component ngOnInit() { // Logout on reaching login screen so we can login this.authService.logout(); } authService logout() { // remove logged in user…
physicsboy
  • 5,656
  • 17
  • 70
  • 119
1
vote
1 answer

Angular unit testing, for Observable errors

I am trying to errors in code coverage for service calls. I am following two approaches but doing some mistake. below is my method for which I am writing test cases setPrefixSuffixDetails(): void { this.prefixSuffixDetailSubscription =…
Developer
  • 279
  • 6
  • 22
1
vote
1 answer

Writing test cases for Http service in angular

Hi I have been trying to write test cases for my registration service. It has a url which it sends 4 values. the service is as follows: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http' interface…
1
vote
1 answer

How to auto reload angular 4 test cases in Linux Machine.

I am using test cases in angular 4 for the frontend. But whenever I do any change in the file, it does not reflect automatically. I need to run "ng test" command after every change that is consuming so much time. Please tell me any way, so that…
rohit13807
  • 605
  • 8
  • 19
1
vote
1 answer

Why angular need browser to run unit tests

When we run the angular unit test it launches chrome browser or which is provided in karma.config.js. Why browser needs and what exactly happened while angular unit tests running.
Rohit Shelhalkar
  • 756
  • 1
  • 9
  • 15
1
vote
1 answer

angular httpClientTestingModule httpMock giving error: found 2 requests

I have written first test to test service class: service class: import { Injectable } from '@angular/core'; import {HttpClient} from '@angular/common/http' import {IComment} from "../../models/comments"; import {Observable} from…
Always_a_learner
  • 4,585
  • 13
  • 63
  • 112