Mock A Function That A Component Imports
// MyComponent.js
import React from 'react';
import { isAuthenticated } from './authentication';
const MyComponent = (props) => {
if (isAuthenticated()) {
return (<div>{/* ... */}</div>);
} else {
return (<div>Not authenticated</div>);
}
};// MyComponent.test.js
// ... various testing imports
import * as authModules from './authentication';
it('renders the component', () => {
authModules.isAuthenticated = jest.fn().mockReturnValue(true);
const wrapper = shallow(<MyComponent />);
expect(toJson(wrapper)).toMatchSnapshot();
});Last updated