首页 > 解决方案 > 开玩笑不使用手动模拟

问题描述

我正在使用 Create React App。我创建了一个使用手动模拟的 Jest 测试。测试呈现我的 App 组件,我正在尝试模拟嵌套组件。它仍在使用原始的 BarChart 组件。

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

我尝试使用

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

但这也没有用。

由于Manual mock not working in with Jest 中描述的问题,我在 beforeEach 中使用了 require 语句

setupTests.js

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

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

标签: jestjsenzymebabel-jest

解决方案


推荐阅读