close

[Solved] Jest: Cannot spy the property because it is not a function; undefined given instead

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Jest: Cannot spy the property because it is not a function; undefined given instead in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How To Solve Jest: Cannot spy the property because it is not a function; undefined given instead Error ?

  1. How To Solve Jest: Cannot spy the property because it is not a function; undefined given instead Error?

    To Solve Jest: Cannot spy the property because it is not a function; undefined given instead Error After hours of debugging, found out that the instance didn't have any methods bound. Since it is a connected component, using shallowWithIntl() and dive() resolved the error.

  2. Jest: Cannot spy the property because it is not a function; undefined given instead

    To Solve Jest: Cannot spy the property because it is not a function; undefined given instead Error After hours of debugging, found out that the instance didn't have any methods bound. Since it is a connected component, using shallowWithIntl() and dive() resolved the error.

Solution 1

After hours of debugging, found out that the instance didn’t have any methods bound. Since it is a connected component, using shallowWithIntl() and dive() resolved the error.

it('should validate remit codes on save', () => {
    const wrapper = shallowWithIntl(<RemitOptionsView
      {...testProps}
    />);
    const button = wrapper.dive().find('Button[text="Save"]'); //Not finding the button
    const instance = wrapper.dive().instance();
    const spy = jest.spyOn(instance, 'validateOnSave');
    instance.validateOnSave();
  });

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment