273 questions
1
vote
1
answer
68
views
Angular unit testing - using debugElement.queryAll to get instances of a button
Running this unit test in Angular 21 always fails with the following error:
× should toggle showSettings when settings button is clicked (440231 ms)
● DrawerComponent › should launch settings ...
0
votes
1
answer
72
views
Angular unit testing relative RouterLinks
In my Angular app I need to be able to unit test that routerLink create the correct href attribute. Sometimes they are links relative to the current route, and sometimes they are absolute links from ...
3
votes
1
answer
1k
views
How to unit-test an Angular v19 `resource` and trigger the evaluation of input-signal changes?
I have a Service which contains a resource (for a list of country-objects).
Unit-testing the usual way seems not to work, even with fakeAsync and tick.
Changes on the Subject which feeds the request-...
0
votes
0
answers
42
views
How to unit test multple HTTP requests in Angular?
I have a simple Angular 19 component that will make multiple requests for SVG files, and then insert them into a component
@Component({
selector: 'app-custom-svg-images',
imports: [AsyncPipe],
...
-1
votes
1
answer
955
views
How use esbuild instead of webpack for unit tests
I have an application, which was generated with Angular CLI v19 and uses the application builder. The application builds and works perfectly fine, however when running ng test, I get a new error:
...
1
vote
0
answers
82
views
Unit test case failed using jest in angular
I am trying to create unit test case for the below code,
export class TestService {
currentContent$ = this.getContent().pipe(
distinctUntilChanged((p, c) => isEqual(p, c))
);
...
0
votes
1
answer
449
views
angular unit testing: element.animate is not a function
I am trying to test mat snackbar. I have following code in my component.ts
snackbarRef: MatSnackBarRef<TextOnlySnackBar>;
constructor(private snackBar: MatSnackBar) {}
onUpdate(){
this....
2
votes
1
answer
1k
views
After updating to Angular 19, some unit tests fail with NullInjectorError: No provider for ChangeDetectorRef
After updating my Angular project (including Angular Material) to v19, I am getting new errors like the following in my unit tests:
NullInjectorError: R3InjectorError(Standalone[MyComponent])[...
8
votes
2
answers
3k
views
Setup global providers for Angular unit tests
I have generated a fresh Angular 18 project and made it zoneless.
It works fine, however in all of my unit tests I am now getting the following error:
Error: NG0908: In this configuration Angular ...
1
vote
1
answer
387
views
TranslatePipe in test with Jest
I'm facing this error when I try to run test in an Angular project:
NavigationMainComponent › should create
TypeError: Cannot read properties of undefined (reading 'subscribe')
at ...
3
votes
1
answer
2k
views
How to add unit tests for Signal Queries?
I am using Angular's signal queries (viewChild, viewChildren, contentChild, contentChildren) like the following:
export class MyComponent {
myScrollContainer = viewChild('scrollContainer', { read: ...
2
votes
0
answers
256
views
Unit test effect in Angular
I am using Angular signal and to execute change in signal using effect function.
How to mock signal to trigger effect during unit test.
import { effect, Injector, Signal } from '@angular/core';
...
0
votes
0
answers
131
views
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL) at <Jasmine>
I am getting following error when ran the unit tests in pipeline for an Angular app.
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
...
3
votes
1
answer
10k
views
Angular 18 - No provider for Http Client used on service for component Unit test
I have an application using Angular 18, and I have one component that uses a service. That service calls HttpClient, gets a list of entities and the component uses that on the constructor.
The unit ...
1
vote
0
answers
2k
views
Angular unit tests are failing with Error: NG0205: Injector has already been destroyed. in http://localhost:9876/_karma_webpack_/vendor.js (line 59623
We upgraded our Angular application from version 16 to 18. After the upgrade, our unit tests started failing with the following error:
Error: NG0205: Injector has already been destroyed. in http://...
0
votes
1
answer
710
views
Angular Unit testing: How to pass multiple files through include while running ng test from CLI?
I am using ng test to run my unit tests.
I have a scenario where I need to test mutliple spec files which are in different paths.
I am able to do this using angular.json
"test": {
...
"...
1
vote
0
answers
90
views
ng test - SyntaxError: Unexpected token '.'
Recently, I upgraded my angular project from v14 to v15. But after the upgrade, on running the test suite using the command ng test, I'm getting the following error:
I addressed the warning shown in ...
2
votes
1
answer
108
views
Angular-jest: single unit test takes 1500 sec to complete
I am new to angular unit testing with jest. And config the project with jest as stated below.
After every --clearCache, running a simple unit test takes 500 to 1500 sec. But after the 1st run unit ...
2
votes
0
answers
51
views
Every Angular unit tests fail on init when added @ContentChildren with component type
I've recently added @ContentChildren to one of my components which are reused throughout the whole application and now I can't get rid of unit tests fail. It fails on init with error:
An error was ...
1
vote
0
answers
2k
views
Unit testing angular component which uses input.required
I'm trying to unit test one of my components which has
regexCtrl = input.required<FormControl<string>>({alias: 'regex'});
After some Googling, this was, more or less, what I thing should ...
0
votes
0
answers
266
views
Mock non-standalone dependencies in standalone components for unit tests
I have a standalone component with some non-standalone dependencies. These dependencies were added with related modules to the imports array. How can I mock dependencies of non-standalone components ...
8
votes
5
answers
2k
views
Angular Unit Test TypeError: this._renderer.addClass is not a function
After updating, my previously passing unit tests now fail with the error: 'TypeError: this._renderer.addClass is not a function' across multiple tests, despite not using or importing the renderer in ...
0
votes
0
answers
58
views
Angular Modal Service Failed "RangeError: Maximum Call Stack Size exceed" issue
Using below service in my project and unit testing failed due to Range Error and where I used this service that component also failed unit test.
Modal.service.ts
import { DOCUMENT } from '@angular/...
1
vote
1
answer
283
views
Angular Tests. ProvidedIn: Module breaking
Currently I am using a service:
@Injectable({
providedIn: MyModule
})
export class MyService {
//... do stuff
}
---
@NgModule({
declarations: [MyComp],
imports: [
ReactiveFormsModule,
...
1
vote
1
answer
713
views
Jest unit test issue when importing a Service from Shared Library in Angular
I have the following test suits
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } ...
0
votes
2
answers
624
views
how to write unit test in angular service that call http in constructor and fill property?
I am new in angular unit testing; I want to write a unit test for the service that is injected in constructor and is called http in it . but I don't know why globalConfig property is undefined:
@...
1
vote
1
answer
124
views
Angular unit testing form
I am trying to unit test a Login Form, but I am not able to execute it as expected.
TestBed configuration:
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule],
...
0
votes
1
answer
446
views
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');
...
0
votes
1
answer
91
views
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
\...
0
votes
1
answer
78
views
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 ...
2
votes
1
answer
1k
views
Angular + Cypress component tests: Retrieving service via InjectionToken
I am writing some component tests for my Nx Angular application using Cypress. I am trying to retrieve an instance of a provider that I set up in my test:
export const DASHBOARD_TOKEN = new ...
1
vote
1
answer
1k
views
NgxsFeatureModule does not have a module def (ɵmod property)
Running NX mono repo and Angular
"@nrwl/angular": "15.2.4",
"@angular/core": "~15.0.0",
"@ngxs/store": "^3.7.6",
"jest": "28....
1
vote
1
answer
1k
views
Unit testing Angular interceptor without Testbed using Jest. How to check headers were appended with bearer token. [Subscription returns nothing]
So I'm attempting to unit test my interceptor with out using testbed. The interceptor merely appends the Auth headers with a bearer token. I can't seem to figure out how to actually check that those ...
1
vote
1
answer
839
views
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 ...
2
votes
2
answers
2k
views
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 <p> with fixture.debugElement.query(By.css)', () => {
...
0
votes
1
answer
981
views
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 ...
0
votes
0
answers
90
views
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
...
0
votes
2
answers
991
views
Angular Testing 'Expected undefined to be truthy.'
This error comes up even with just the boiler plate for the spec file. Is there any other issues causing this?
ts file
import { Component, OnInit, Input, Output, EventEmitter, ViewContainerRef } from '...
0
votes
2
answers
379
views
Cannot trigger click event undefined element
I need to test click event on mat-table row. Below is my mat-table code
<table
mat-table
[dataSource]="dataSource"
matSort
matSortActive="formCode"
...
0
votes
1
answer
1k
views
Angular spectator testing: target component-level service
In my Angular app I have a service which is provided at a component level:
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
providers: [MyService],
})
export class ...
0
votes
1
answer
191
views
How to write unittest for a class use socket.io-client
I have a class which use Socket as a private property.
import {Socket} from 'socket.io-client';
export class SocketService {
private socket: Socket;
public initializeSocket() {
if(this....
0
votes
2
answers
942
views
How to test the method of a private property is called in Angular
I have a class which has a private property.
import {Socket} from 'socket.io-client';
export class SocketService {
private socket: Socket;
public initializeSocket() {
if(this.socket) { ...
0
votes
2
answers
612
views
Getting cannot read properties of undefined (reading:'viewMode') in Angular Unit Testing
Hi I have a function which I would like to unit test. This is my function.
let docProcess: IDocProcess;
public onCalling(args) {
if (this.docProcess.viewMode) {
this.makeCalls(args);
}
}...
0
votes
0
answers
566
views
Getting error : : Cannot read properties of undefined (reading 'deps') while running angular test file
Please find the describe in test file :
`
fdescribe('NdelDeviceBalancePayoffComponent', () => {
let component: NdelDeviceBalancePayoffComponent;
let fixture: ComponentFixture<...
0
votes
1
answer
2k
views
Angular unit testing check if input is undefined
I am unable to test else condition in my code as Unable to change value of input(). Please help.
Once I pass data in spec.ts unable to assign other data
@Input() data: any;
ngOnInit(): void {
if (...
0
votes
1
answer
123
views
After completion of ng test, getting Uncaught [object Object] at /_karma_webpack_/polyfills.js
Test cases are running successfully but at the end it gets returned with an Uncaught [object Object] error which looks like this: -
Test script: ng test --codeCoverage=true --no-watch --no-progress --...
0
votes
1
answer
223
views
Observable test function call in pipe tap
I'm trying to cover this.httpCache.put(this.agreementUrl, agreement as any); line in test but it always fails,
public getTransportAgreement(): Observable<TransportAgreement> {
const ...
2
votes
0
answers
418
views
Negative scenario creating conflict with positive angular unit testing
I've written the test case for services where I'm testing Negative and positive scenarios both but giving me errors for the negative scenario.
I've written in order first I'm checking the negative ...
-1
votes
1
answer
752
views
How to test custom event emitter service in Angular 12
I have implement component communication using emit emitter service.
service.ts
import { EventEmitter, Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ...
0
votes
1
answer
318
views
how to write Angular12 unit test with Private method and local variables
export interface UserDetails {
name: 'string',
id: 'string',
address: {
area: 'string',
city: 'string',
}}
export class UserComponent {
Private GetData() {
let userDetail: UserDetails = {
name: 'user ...