I am trying to test whether componentWillMount was called and for that my test is
test(\'calls `componentWillMount` before rendering\', () => {
let fn =
I don't know if the other answers have helped with your question, but you shouldn't need to test componentWillMount. React should already do that testing for you.
More relevant to your testing would be to test the functions or actions you are putting in that method for your component.
If you are making some API call, running a function based on props, or anything else, that is what you should be testing for. Mock the function/action/code that componentWillMount triggers, and make assertions and expectations on that.
Example:
Component:
class YourComponent extends Component {
componentWillMount() {
/*this fetch function is actually what you want to test*/
this.props.fetch('data')
}
render() {
/* whatever your component renders*/
}
}
Tests:
test('should call fetch when mounted', () => {
let mockFetch = jest.fn()
const wrapper = mount();
expect(wrapper).toBeDefined();
expect(mockFetch).toHaveBeenCalled();
expect(mockFetch.mock.calls[0]).toEqual(['data'])
});