Why does Angular 15 unit test fail with "Cannot access properties of a readonly constant"?

I've encountered an issue while trying to run unit tests with Angular 15.2.10 and Karma 6.3.12, even after attempting Karma 6.4.3 without success.
Strangely, the error occurs in a readonly constant rather than a variable.
Interestingly, the code runs perfectly when executed with ng serve

Lets take a look at the complete stack trace:

27 03 2024 17:19:00.500:WARN [karma-server]: `autowatch` and `singleRun` are both `false`. In order to execute tests use `karma run`.
√ Browser application bundle generation complete.
27 03 2024 17:19:19.887:INFO [karma-server]: Karma v6.4.3 server started at h t t p : / / localhost:9876/
27 03 2024 17:19:19.890:INFO [launcher]: Launching browsers Edge with concurrency unlimited
27 03 2024 17:19:19.895:INFO [launcher]: Starting browser Edge
27 03 2024 17:19:28.277:INFO [Edge 122.0.0.0 (Windows 10)]: Connected on socket ZJvt9iD1pJGZzp_IAAAB with id 72359205
Edge 122.0.0.0 (Windows 10) ERROR
  An error was thrown in afterAll
  Uncaught TypeError: Cannot read properties of undefined (reading 'restCallType')
  TypeError: Cannot read properties of undefined (reading 'restCallType')
      at Function.<static_initializer> (localhost:9876/_karma_webpack_/webpack:/src/app/commons/core/utility/constants.ts:613:20)
      at Module.20467 (localhost:9876/_karma_webpack_/webpack:/src/app/commons/core/utility/constants.ts:6:23)
      at __webpack_require__ (localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.84225 (localhost:9876/_karma_webpack_/main.js:5733:89)
      at __webpack_require__ (localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.55041 (localhost:9876/_karma_webpack_/main.js:63:98)
      at __webpack_require__ (localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.24882 (localhost:9876/_karma_webpack_/main.js:14:72)
      at __webpack_require__ (localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at __webpack_exec__ (localhost:9876/_karma_webpack_/main.js:27950:48)

groups-search.component.spec.ts

import {ComponentFixture, TestBed} from '@angular/core/testing';

import {GroupsSearchComponent} from './groups-search.component';

describe('GroupsSearchComponent', () => {
    let component: GroupsSearchComponent;
    let fixture: ComponentFixture<GroupsSearchComponent>;

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [GroupsSearchComponent]
        })
            .compileComponents();
    });

    beforeEach() => {
        fixture = TestBed.createComponent(GroupsSearchComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

constants.ts

import {Injectable} from '@angular/core';

@Injectable()
export class Constants {
    
    ... some other constants ...
    
    public static readonly restCallType: any = {
        GET_ALL: "GET_ALL",
        ... other values ...
    }
    
    ... some other constants ...
    
    public static readonly services: any = {
        GET_ALL_GROUPS: {
            code: "GET_ALL_GROUPS",
            type: Constants.restCallType.GET_ALL,
            url: "api/v1/group/findByParams"
        }
    }
    
    ... some other constants ...
    
}

Upon replacing this line:

type: Constants.restCallType.GET_ALL,

with this:

type: "GET_ALL",

The issue gets resolved, but the reason remains unclear.
Would appreciate any assistance.
Thank you

Answer №1

The property Constants.services relies on Constants.restCallType being initialized beforehand. But, it currently attempts to access Constants.restCallType before initialization. One solution could be to modify the services getter method to be static:


    public static get services(): any {
        return {
            GET_ALL_GROUPS: {
                code: "GET_ALL_GROUPS",
                type: Constants.restCallType.GET_ALL,
                url: "api/v1/group/findByParams"
            }
            // ... other services ...
        }
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to manually trigger a re-render of a component?

I am new to Angular 2 and I'm accustomed to the digest cycle in Angular 1. In Angular 1, when I update the scope of a view, I can manually trigger a digest by calling $scope.$digest(). However, in Angular 2, with its lack of implicit data binding, I&a ...

Navigating through object keys in Angular 5 by iterating through them

I have an object with protected products: protected products: { [key: string]: { color: string, brand: string, }; } = {}; products = { scan12345: {color: "Orange", brand: "X"}, scan13813: {color: "Pink", brand: "X"}, } How can I loop through t ...

What could be the reason behind Next.js attempting to import a non-existent stylesheet?

Lately, I've come across an issue where Nextjs is attempting to import a non-existent stylesheet (refer to the images below). I'm looking for guidance on how to resolve this error. Hoping to find a solution here, thank you Web browser console W ...

Troubleshooting Angular 2 Ahead-of-Time Compilation Heap Errors

Encountering " JavaScript heap out of memory" error during AoT compilation I have been following the AoT guide provided on angular.io to compile my project. Shown below is the typescript configuration file I am using for AoT: { "compilerOptions": { ...

Ways to funnel Firebase API endpoint towards an outside web address

When hosting on Firebase, how can I redirect all API requests from https://<domain>.web.app/api/v1/db/search?query=<something> To: http://<domain>:<port>/v1/db/search?query=<something> I've searched extensively online a ...

Acquiring a JQuery object within Angular: A step-by-step guide

Please review the code snippet below: protected get myModalDialogRef(): JQuery { return $('#myModalDialog'); } What dependencies do I need to install in order to specify JQuery as the return type? I noticed in the package.json file, there i ...

Caught off guard by this promise: TypeError - Attempting to access a property that does not exist in my Ionic 2 application

I'm encountering an issue with native Facebook login that displays the following error message: Uncaught (in promise): TypeError: Cannot read property 'apply' of undefined https://i.sstatic.net/PDWze.jpg I have shared my entire project ...

Exploring Angular's wild comparison capabilities for strings within HTML documents

I have a question that may seem basic, but I am curious if it is possible to use wildcards in *ngIf comparisons. Consider the following code snippet: <ng-container *ngIf="test == 'teststring'"> </ng-container> I am wonder ...

Angular reference numbers vs ngModel

When is it necessary to use [(ngModel)] on an input, and when can I simply use #reference? For instance: <div class="form-group"> <div class="input-group mb-2"> <input type="text" class="form-control" [(ngModel)]="newUser"> < ...

Encountering Angular 8 error TS2304 at line 39: Cannot find the specified name after shutting down WebStorm

After working on my Angular project and closing the IDE last night, I encountered an odd problem today. The project doesn't seem to recognize certain libraries such as Int8Array, Int16Array, Int32Array... And many others. While the project is still ab ...

Creating a Type Definition for Various Record Patterns in Typescript

Seeking guidance on using Nivo charts with TypeScript, specifically defining the type of data Nivo expects for their Bar Chart (https://Nivo.Rocks). I have experimented with: Object Index Signature ... {[Key: string]: string;} Record utility type ... ...

Error in Angular 8: Module loading from "url" has been intercepted due to an unsupported MIME type ("text/html") being blocked

I have developed an application using Angular 8 and Node 12. When I try to open a new tab or reload the page after building Angular 8, I encounter an issue for which I have extensively searched online but found no solution. In the Firefox console, the er ...

Typescript error: RequestInit not properly initialized

I'm encountering an issue while using fetch to call an API in a typescript file. The browser is throwing an error stating that const configInit must be initialized, even though I believe it is already. Any suggestions on how to resolve this? Thank you ...

Ways to verify that express middleware triggers an error when calling next(error)

I attempted to capture the error by using next() when stubbing it, but unfortunately it did not work. Below is the function: async getUser (req, res, next) { try { if (!req.user) { throw new CustomError('User not found', 404) } ...

Issue with Angular custom tag displaying and running a function

I have created a custom HTML tag. In my next.component.ts file, I defined the following: @Component({ selector: 'nextbutton', template: ` <button (click) = "nextfunc()">Next</button> ` }) export class NextComponent{ nextfunc( ...

Create an Angular material table with expandable rows that become sticky when scrolled past, then automatically unstick once they are no longer in view

Currently, I am working with Angular Material Table v11.1.0 which includes a main row with expandable rows. I want the main row to become sticky once an expandable row is opened and remain sticky while scrolling through the table. My goal is for the main ...

Creating a custom mdToast in Angular material with added functionality: A step-by-step guide

Currently, I am working on a project using Angular 1.x and the Material UI framework. While building, I encountered an unusual issue where I am unable to create a custom Toast (snackbar) with functioning buttons: /// <reference path="../../_All.d.ts"/& ...

When working with Typescript and Vue.js, it's important to ensure that properties are initialized before

Check out the following code snippet: export default class PrimitiveLink extends Vue { style = { // Reset display: 'inline-block', textDecoration: 'none', outline: 'none', // Theme ...this.themeStyle ...

Dynamic parameterization of Angular form controls

I'm facing an issue with adding validators to a form where some controls are required only if a specific condition is met by another control. I initially created a custom validator function that takes a parameter to determine the requirement, but the ...

Tips for circumventing if statements in TypeScript-ReactJS?

I currently have various action checks in place to perform different functions. Essentially, I am using if and else conditions to determine the action type and execute the corresponding functionality as shown below: public onMessage = (messageEvent) => ...