【发布时间】:2018-10-17 18:03:07
【问题描述】:
我通过 create-react-app 创建了一个新的 React 应用程序,我想为我在应用程序中创建的名为“MessageBox”的组件编写一个单元测试。这是我写的单元测试:
import MessageBox from "../MessageBox";
import { shallow } from 'enzyme';
import React from 'react';
test('message box', () => {
const app = {setState: jest.fn()};
const wrapper = shallow(<MessageBox app={app}/>);
wrapper.find('button').at(0).simulate('click');
expect(app.setState).toHaveBeenLastCalledWith({modalIsOpen: false});
});
我还在“src”文件夹下添加了一个名为“setupTests.js”的文件,内容如下:
import * as enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';
enzyme.configure({ adapter: new Adapter() });
我运行它:
npm 测试
我得到了错误:
Enzyme 内部错误:Enzyme 需要配置适配器,但是 没有找到。要配置适配器,您应该调用
Enzyme.configure({ > adapter: new Adapter() })
你知道什么可以解决这个问题吗?
【问题讨论】:
-
截至此评论日期的当前错误消息还包含文档的 URL。访问该 URL 主要解释了要做什么。
-
@CrazySynhax,请接受来自“@Mikel”的另一个答案,因为它使配置共享,您的测试看起来会更清晰。在每个测试中都有这样的显式配置太冗长了。
标签: reactjs enzyme create-react-app