Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save timothyjellison/a9c9c2fdfb0b30aab5698dd92e901b24 to your computer and use it in GitHub Desktop.
Save timothyjellison/a9c9c2fdfb0b30aab5698dd92e901b24 to your computer and use it in GitHub Desktop.
Mocking a component constructor with Jest
// An answer for this stackoverlow question: https://stackoverflow.com/questions/48831701/jest-how-to-mock-default-export-component-when-same-module-also-has-named-expor
import React from 'react';
import { shallow } from 'enzyme';
import App, { myUtilityFunction } from './App';
jest.mock('./App');
App.mockImplementation(() => {
return {
render: () => <div>MockApp</div>
};
});
myUtilityFunction.mockImplementation(() => 'foo');
it('renders the mock React component', () => {
/**
* PASSES
*/
expect(shallow(<App />).getElement()).not.toEqual(null);
});
it('runs the utility method correctly', () => {
/**
* PASSES
*/
expect(myUtilityFunction()).toEqual('foo');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment