Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest: Cannot read property then of undefined

I have situation in my unit test case for a react application, where in a function calls for another function received in props from parent component. The parent component functions definition is something like this:

onSavePropClick(action) { 
    const save = this.saveProperty(action);
    if(action === SAVE){
        return () => new Promise(() => {
            resolve(this.calculate().then(save));
       });
    }
return save;
}

This function call has been passed as props to the child component as

<MyComponent finalSave={this.onSavePropClick(SAVE)} onClose={()=>this.setState({closeWindow: true})} />

MyComponent has a function:

savingAndShowResults() {
const { finalSave, onClose } = this.props;
finalSave().then(() => {
onClose();
});
return true;
}

Now when I have a test for the executed, it throws me error as “Cannot read property then of undefined”, the test is as follows

const initialProps={
finalSave: jest.fn(),
onClose: jest.fn()
};

it(‘should handle saving and show results’, () => {
const component = shallow(
<MyComponent {...initialProps} />
);
component.instance().savingAndShowResults();
expect(initialProps.finalSave).toHaveBeenCalled();
expect(initialProps.onClose).toHaveBeenCalled();
});

I am not able to figure out why even on resolving in return in promise of Parent component’s function, gives me this error. Please suggest.

like image 291
OM The Eternity Avatar asked Mar 14 '20 10:03

OM The Eternity


People also ask

Can not read the property then of undefined?

What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.

Can not read the property of undefined In react native?

The "Cannot read property 'props' of undefined" error occurs when a class method is called without having the correct context bound to the this keyword. To solve the error, define the class method as an arrow function or use the bind method in the class's constructor method.

How do you mock .then in jest?

In order to mock asynchronous code in Jest, more specifically Promises, you can use the mockResolvedValue function. This will mock the return value of the Promise to be 42. In order to test a Promise in Jest, you need to turn your it block into async in order to use the await keyword in front of an expect statement.

How do you mock a function?

There are two ways to mock functions: Either by creating a mock function to use in test code, or writing a manual mock to override a module dependency.


1 Answers

Assuming initialProps.finalSave is a mock function, you need to make sure you're returning a promise from initialProps.finalSave:

const initialProps = {
  finalSave: jest.fn().mockImplementation(() => Promise.resolve());
  ...
};
like image 84
helloitsjoe Avatar answered Oct 03 '22 08:10

helloitsjoe