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

Include an angular module in all.spec files via karma

I have an angular module that has some angular configuration that I want all tests to have before running. I could go in all the files and include that module via angular.mock.module but I was wondering if I could do it via karma somehow.
fur866
  • 413
  • 1
  • 5
  • 14
1
vote
0 answers

Unit test Angular Directive using ngModelChange

I have a PhoneMask directive that I apply to an input and it will strip out non-numeric characters and format the number according to the mask I set up (e.g. an input of 1234567890 will be formatted as (123) 456-7890). The mask itself works, but I'd…
jloosli
  • 2,461
  • 2
  • 22
  • 34
1
vote
0 answers

Angular unit test: Can't resolve all parameters for component

OK, so I've been trying to figure this out for a while now, and all I can find online (regarding the error message in the title) refers to circular dependencies during DI - but I'm 99% certain that's not my issue. So, here goes - I get the following…
Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
0
votes
1 answer

How to trigger select event in openlayers map in angular for unit test?

How can I trigger a select interaction on openlayer map in angular unit testing? below is my select event code. let select = new Select({style: null}); select.set('selectInteraction', 'name'); select.on('select', (e) => { …
Prabhakaran
  • 1,264
  • 2
  • 20
  • 47
0
votes
1 answer

Is it possible to test a grandchild component in angular tests?

Right now we are using karma testing to check for existing buttons in a component, the thing is the component displays the buttons as the child of a child. what we are testing is: ProductNavComponent \ NavComponent \ ButtonComponent we want to test…
0
votes
1 answer

Jasmine won' detect cancel event on dialog

I'm trying to implement a dialog component where user can click escape button to close the popup (I'm using DaisyUI). It works fine when I try it manually, but for some reason I'm not able to create the unit test for it. It fails. Unit Test: …
0
votes
0 answers

Getting error while running Jest test case in Angular

I created an Angular application, uninstalled Jasmine and Karma and installed Jest. I have done all the steps correctly. Whenever I run the jest tests it gives me an error: "error TS5023: Unknown compiler option 'files'". I am unable to figure out…
Akrit
  • 91
  • 1
  • 4
0
votes
0 answers

Angular Unit test for some complicated method (its not a class or component)

export const getRowSettlementDate = ( tradeRow: Models.TradeRow, deal: Models.DealDto, validationService: Services.ValidationService ): Date | null => { const notValidValue =…
0
votes
0 answers

Error: Expected one matching request for criteria "Match URL: in angular unit testing?

I am creating the test case for the component but i got above error in the cmd after ng test.i have tried each and everything but nothing solution worked for me check below code snippet in which i have shared the function which need to be…
Kapil Soni
  • 1,003
  • 2
  • 15
  • 37
0
votes
0 answers

Angular unit tests - mock service with observable

I'm trying to fix my unit tests of a component that subscribes to an Observable of the service DataService during ngOnInit(). However, at the moment I'm still getting TypeError: Cannot read properties of undefined (reading 'subscribe'). I tried to…
LukyFoggy
  • 519
  • 8
  • 31
0
votes
0 answers

Angular Unit Test when we called Modal with @viewChild

I am very new in karma jasmine test case. please help me out. Typescript import { Component, OnInit, ViewChild } from '@angular/core'; @Component({ selector: 'app-label', templateUrl: './app-label.component.html', }) export class LabelRenderer…
Swapnil Yeole
  • 406
  • 3
  • 12
  • 26
0
votes
0 answers

Mock or spyOn location.reload in angular unit testing

I have the function with location.reload(). How to write the angular unit test cases for test that reload functionality. searchregion(){ location.reload(); } I tried to written the test cases like below. But it shows error "Error: : could not find…
VelPaari
  • 11
  • 2
0
votes
2 answers

by.css always returns null [jasmine unit test ]

I have no idea why in my Angular project, Jasmin alwasys return null when I'm trying to access a HTML's element! it('should find the

with fixture.debugElement.query(By.css)', () => { const bannerDe: DebugElement = fixture.debugElement; …

0
votes
1 answer

spy on method when unit testing http interceptor in angular

I'm trying to unit test an http interceptor. The interceptor is used for authorization. I can test the basic success case, but I'm trying now to test the case where the API call fails because access token is out of date. My question is, how do I…
Mr Smith
  • 3,318
  • 9
  • 47
  • 85
0
votes
0 answers

Angular Karma/jasmine Unit testing, InjectorToken , RxJs

Im a Angular newbie and starting to write Unit Tests. I have a simple Component, which loads data from RxJs - Subject. Im not able to initialize the spec.ts file for testing does anyone now how to do the correct implementation ? Thats my…