Jest not using manual mock

2019-06-07 13:13发布

问题:

I'm using Create React App. I created a Jest test that uses a manual mock. The test renders my App component and I'm trying to mock a nested component. It's still picking up the original BarChart component.

containers/__tests__/App.js
import React from 'react';
import { shallow, mount } from 'enzyme';

const barChartPath = '../../components/d3/BarChart/BarChart';

describe('App', () => {
  beforeEach(() => {
    const mockBarChart = require('../../components/d3/BarChart/__mocks__/BarChart').default;
    jest.mock(barChartPath, () => mockBarChart);
  });

  it('renders without crashing - deep', () => {
    const App = require('../App').default;
    mount(<App />);
  });
});

I tried using

import BarChart from '../../components/d3/BarChart/BarChart';
...
 beforeEach(() => {
    jest.mock(BarChart) 

but that didn't work either.

I'm using the require statement in the beforeEach due to the issue described in Manual mock not working in with Jest

setupTests.js

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });