Jest not using manual mock

混江龙づ霸主 提交于 2019-12-23 03:44:15

问题


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() });

来源:https://stackoverflow.com/questions/53488371/jest-not-using-manual-mock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!