Is there a way to use jest.mock
's in React? For example, say I want to re-use a component (e.g. Storybook) but want to mock an import / implementation detail for that original React component. Is this possible, and if so, how? Thanks!
sampleComponent.js:
import React from 'react';
import sampleFn from './sampleFn';
const SampleComponent = () => <h1>{sampleFn()}</h1>; // expected output: 'Hello World'
export default SampleComponent;
sampleMockComponent.js:
import React from 'react';
import SampleMockComponent from './sampleComponent';
jest.mock('./sampleFn');
const SampleMockComponent = () => <SampleComponent />; // expected output: 'Testing Mock'
sampleFn.js:
export default () => 'Hello World';
__mocks__/sampleFn.js:
export default () => 'Testing Mock';