jeudi 17 décembre 2020

Angular Test Jest - TypeError: Cannot read property 'queryParams' of undefined

I've seen the same error in other posts but they didn't work for me.

I have an Angular component, where I need to read a queryParam from the url, for example in http://localhost:4200/sample-page?page=3 I want to stores the number 3 into a component local variable.

/**
 * Set page by url parameter
 */
export const setPaginationMarkByUrlParam = (activatedRoute): number => {
  // Get page param from Url to set pagination page
  const pageParam = activatedRoute.snapshot.queryParams;
  return pageParam.page ? Number(pageParam.page) : 1;
};

This function is in another file and I put as parameter the activeRoute, which comes from the ngOnInit of the component in which I want to get the queryParam.

ngOnInit() {
    this.page = setPaginationMarkByUrlParam(this.activatedRoute);
}

This code works perfectly, but when the Jenkins pipeline runs the npx jest tests, I get the following message: TypeError: Cannot read property 'queryParams' of undefined

I don't have any kind of private variables, the activeRoute that comes as a parameter, I tried it with public and private.

Looking at both StackOverflow and GitHub Issues I have not been able to fix this problem.

Thank you very much!

Aucun commentaire:

Enregistrer un commentaire