0

In my angular appliation, I have a config file where I defined some property. In my component I have a method which makes use of that property. I am writing a test case for that method, when I am trying to use spyOnProperty on that property, I get an error saying Error: : getRequestInfo is not declared configurable. Below is the component and spec file.

component.ts

    getRequestInfoDetails(config: MyConfig): void {
        if (config.update) {
            const payload: Partial<IRequestInfoRequest> = getRequestInfo(
                this.form.getRawValue(),
                config.update
            );          
        }   
    }

MyConfig.ts

export const getRequestInfo = (
    form,
    status: string
): Partial<IRequestInfoRequest> => {

     // business logic
}

Spec.ts

 it('getRequestInfoDetails to have called', () => {
    const myFrmgrp = new FormGroup({
    }); 
    const config: MyConfig = {
    form: myFrmgrp,
    status: 'true'
    };
    const request: Partial<IRequestInfoRequest> = {};
    const spyObj = spyOnProperty(config, 'getRequestInfo', 'get').and.returnValue(null);
    component.getForm(config);
    expect(spyObj).toHaveBeenCalled();
});
user1015388
  • 1,283
  • 4
  • 25
  • 45

1 Answers1

0

As TypeScript evolved, this is no longer possible (https://github.com/jasmine/jasmine/issues/1414). The best solution I have found is to wrap the function in a class for the unit tests (https://stackoverflow.com/a/62935131/7365461).

MyConfig.ts

class ConfigWrapper {

  static getRequestInfo = (
    form,
    status: string
  ): Partial<IRequestInfoRequest> => {

     // business logic
 }
}

To use it, you can do:

getRequestInfoDetails(config: MyConfig): void {
        if (config.update) {
            const payload: Partial<IRequestInfoRequest> = ConfigWrapper.getRequestInfo(
                this.form.getRawValue(),
                config.update
            );          
        }   
    }

Then in the test you can do:

spyOn(ConfigWrapper, 'getRequestInfo').and.returnValue(null);
AliF50
  • 16,947
  • 1
  • 21
  • 37