I don’t know if other answers helped your question, but you don’t need to test the WillMount component. The reaction should already do this testing for you.
More appropriate for your testing would be to test the functions or actions that you put into this method for your component.
If you make an API call, run a props-based function, or something else, this is what you should test. Select the function / action / code that componentWillMount
calls the triggers, and make statements and expectations about it.
Example:
component:
class YourComponent extends Component { componentWillMount() { this.props.fetch('data') } render() { } }
Tests:
test('should call fetch when mounted', () => { let mockFetch = jest.fn() const wrapper = mount(<SomeComponent fetch={mockFetch}/>); expect(wrapper).toBeDefined(); expect(mockFetch).toHaveBeenCalled(); expect(mockFetch.mock.calls[0]).toEqual(['data']) });
source share