0

I have a helper file, which contains only one function, which uses transloco translate method.

translate.utils.ts

  import { translate } from "@ngneat/transloco";
  import { SelectItem } from "primeng/api";

  export function someFunction(param: any[]): any[] {
    return param.map((obj:any) => {
      const newLabel= translate(obj.label as any);
      obj.label = newLabel;
      return obj;
    }) as any;
  }

I have imported this function in my angular component

Like this

import { someFunction } from '../utils/translate.utils';
..
//somewher in the component used as 

data = someFunction(SOME_OPTIONS)

while running the test, i'm getting this error

TypeError: Cannot read properties of undefined (reading 'translate')

how to mock someFunction? or fix this translate?

Test were coded in jasmine

Tried mocking like

  1. spyOn(window, "translate").and.callFake((label: string) => Translated ${label});

  2. import * as translate from '../utils/translate.utils' spyOn(translate, "translate").and.callFake((label: string) => Translated ${label});

nothing works, any leads would be appreciated. Thanks

Ranjith Varatharajan
  • 1,596
  • 1
  • 33
  • 76
  • Unfortunately, as TypeScript evolved, it's not possible to spy on imported function the way you have tried in Jasmine. Here is a long Github issue on the topic (https://github.com/jasmine/jasmine/issues/1414) and here is the best solution I have found for this issue (https://stackoverflow.com/a/62935131/7365461). – AliF50 Jun 05 '23 at 12:58

0 Answers0