Karma-Jasmine encountered an issue with a TypeError, stating that it cannot read properties of undefined while attempting to read 'get'

I've been struggling to write a unit test for my Angular component due to an error I can't seem to resolve. Despite my efforts to find a solution on Stack Overflow and various online documentation, I haven't had any luck yet. Here are some of the resources I've consulted:

Below, you'll find the code snippets:

.ts file

export class SomeComponent implements OnInit {
  id = '--';
  
  constructor(
    private readonly someService: SomeService,
    private readonly utilsService: UtilsService,
    private readonly router: Router,
    readonly route: ActivatedRoute,
  ) {}

  // Methods and functionalities implementation
}

.ts.spec

class mockHttpWrapperService {
  readonly isApiReady = false;
}

// Unit testing setup for SomeComponent

And here's the specific error message I'm encountering:

Chrome 93.0.4577 (Linux 0.0.0) SomeComponent should create 1  FAILED
TypeError: Cannot read properties of undefined (reading 'get')

Answer №1

During your testing configuration, you have included the following active route:

activeRouteFake = {
  snapshot: {
    paramMap: convertToParamMap({
      user_id: '123abc',
      company: 'Apple',
    })
  }
};

Then, in your component's ngOnInit method, you attempted to retrieve the parameter like this:

this.userId = this.route.snapshot.queryParamMap.get('user_id');

However, the fake route object does not contain a queryParamMap property, only a paramMap. You should either rename it in your fake object or provide both paramMap and queryParamMap if you require both in your code implementation.

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

Demonstrating the transformation of child elements into parent elements through angular 6 ngFor

I have a JSON array dataset where each object may contain nested arrays. In order to display the inner nested array elements as part of the parent array using Angular's NgFor, I need to format the input like this: [{ 'id': 1, 'tit ...

Issue arises with library dependencies: various libraries are reliant on distinct versions of a shared library

I have multiple libraries that are dependent on the webpack library. Currently, I am using version 4.79.1, but when I run `npm install` I receive the following warning: [email protected] requires a peer of webpack@^2.0.0 || ^3.0.0 but none is in ...

Ubuntu 16 is having difficulty identifying ng commands at the moment

It appears that the 'ng' program is not currently installed on this system. To install it, you can use the following command: sudo apt install ng-common I encountered no errors during the installation process. How should I go about resolving thi ...

Have the validation state classes (such as .has-error) been removed from Bootstrap 5?

I've noticed that the validation state classes (.has-success, .has-warning, etc) seem to have been removed in bootstrap 5 as they are not working anymore and I can't find them in the bootstrap.css file. Before, I could easily use these classes w ...

What is the best approach for unit testing a function that instantiates a ZendDbSqlSql object?

Unit testing a method that creates an Sql object internally can be challenging since it cannot be easily mocked. One possible solution is to avoid making the Sql object a property of the class instance to prevent potential errors. Resetting the Sql object ...

Is it no longer possible to instantiate an object using the syntax <interface>{}?

There seems to be an issue with the code snippet below when run in the TypeScript playground: interface Person { name: string; age: number; } const makePerson= function(name : string, age : number) : Person { let obj = <Person>{} ...

Build a Google Map Widget within SurveyJs

Hey there, I'm new to working with SurveyJS and I'm trying to incorporate a Google Map widget into my SurveyJS. I followed some steps and successfully added the map in the Survey Designer section, but unfortunately, it's not loading in the T ...

What is the best way to extend a class in NestJS from another class?

I've encountered an issue while attempting to extend a class service from another class service in NestJS and utilize DI to load it in a third service. The error message I'm receiving from Nest is: Error: Nest can't resolve dependencies of ...

Transforming JavaScript code with Liquid inline(s) in Shopify to make it less readable and harder to understand

Today, I discovered that reducing JavaScript in the js.liquid file can be quite challenging. I'm using gulp and typescript for my project: This is a function call from my main TypeScript file that includes inline liquid code: ajaxLoader("{{ &ap ...

Exciting updates revealed upon new additions (angular)

I'm working with three components in my Angular application: app.component, post-create.component, and post-list.component. Let's take a look at the structure of these components: <app-header></app-header> <main> <app-post-c ...

Assign a value to a FormControl in Angular 6

I have 60 properties linked to 60 controls through the mat-tab in a form. When it comes to editing mode, I need to assign values to all the controls. One approach is as follows: this.form.controls['dept'].setValue(selected.id); This involves l ...

Creating tests for Vue components that mock the $route object

When testing a component that includes a statement like this.$route.fullPath, how can I mock the value of fullPath for the $route object in order to properly test the component? ...

The optional parameters could not be obtained

After reading through the Angular documentation, it seems that I should be able to retrieve a route's options parameters from route.paramMap. However, when I attempt to log out the params, I am getting an empty object in return. I am developing a log ...

When adjusting the month/year, the Material Date Picker extends beyond its container

Currently, I have an Angular 18 application with a page that includes a material date picker component. When I open the Date Picker, everything displays correctly. However, when I try to change the month using the left/right arrow or the year, the data co ...

"Guidance on setting up my input text box in Angular to allow for selection of required fields

I'm currently in the process of developing a form where all fields are required. My goal is to have the Next button move on to the subsequent form only if all required fields have been filled out. However, I seem to be encountering an issue where desp ...

Issue: The system needs the 'image' property to proceed (Dall E's image variation API by OpenAI)

Utilizing the Dall E Create image variation API has been a bit challenging for me. Every time I send a post request, I encounter this particular error: error: { "code": null, "message": "'image' is a required property&q ...

Confusing generic function overload in TypeScript

During my exploration of TypeScript, I came across an unusual situation. function concat5<T>(strs: T, strs2: T): T; function concat5(strs: string, strs2: string) { return strs + strs2; } concat5(123, 12); concat5({a:1}, {b:2}); Upon reviewing ...

How can I enable dragging functionality for components (not elements) in angular?

While utilizing the Angular CDK drag and drop module, I have observed that it functions seamlessly on html elements like div, p, etc. However, when I apply a cdkDrag directive to a component, it does not seem to work as expected. <!-- IT WORKS --> ...

How can I retrieve the value of a promise in Promise.map?

I am currently working on a project that involves saving data to a database using Mongoose. One specific field in the database is the 'thumbnail' field, which needs to be filled with a base64 converted file after the file is uploaded to the serve ...

Is it possible to verify or authenticate the properties received directly from the associated type or interface?

Looking for a more efficient way to handle validation in my component that takes an array of tabs and children as props. I would like to check if the children provided are the same length as the tabs array directly from the type declaration or any cleaner ...