Angular - Jest : spyOnProperty (similar to jasmine)

Viewed 853

Please guide me create to SpyOnProperty (like Jasmine) in Jest

I am spying on a property on a child class in Jest however there is no predefined thing like spyOnProperty() from jasmine in jest.

How to spy on the property of a child component?

component

Class Parent {
       @ViewChild(ButtonComponent) btn: ButtonComponent;
}

spec

describe('ParentComponent', () => {
    //.............

    fixture = TestBed.createComponent(ParentComponent);
    component = fixture.componentInstance;
    spyOnProperty(component.btn, 'state').and.returnValue('active); // jasmine
    jest.spyOn(component.btn, 'text', 'get').mockReturnValue('bar');// jest -> INVALID
    fixture.detectChanges();

    //----------
});

error

Uncaught (in promise): Error: Cannot spyOn on a primitive value; undefined given Error: Cannot spyOn on a primitive value; undefined given

           fixture = TestBed.createComponent(ParentComponent);
           component = fixture.componentInstance;
       >   jest.spyOn(component.btn, 'searchText', 'get').mockReturnValue('bar');
                   ^
           fixture.detectChanges();
       }));
0 Answers
Related