jestjs - 开玩笑不使用手动模拟
问题描述
我正在使用 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() });
解决方案
推荐阅读
- javascript - 尽管字符串长度,如何保持旋转的横幅文本垂直居中?
- apache-spark - 在火花结构化流中从 kafa 读取时,有什么方法可以解析 json(不解析模式)
- java - 基于给定 XSD 文件将 Java 对象转换为 XML 的问题
- javascript - 带有时间戳的多行文本。将每个字符串的时间戳附加到每行的末尾(因此每行两次)
- c++ - 无法从另一个类调用静态函数
- ios - 一旦在 IOS 上安装,Phonegap 构建不会要求任何权限
- python - 使用 mysql.connector for Python 在 (%s %s %s) 附近出现语法错误
- java - 我不想输入列表的大小,但我也想在列表中动态添加数字
- javascript - 在单击事件处理程序回调函数后设置测试的布尔状态
- php - 我应该使用什么作为 Cognito 的 UserContextData => EncodedData?