相关疑难解决方法(0)

Enzyme需要配置适配器

我通过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});
});
Run Code Online (Sandbox Code Playgroud)

我还在'src'文件夹下添加了一个名为'setupTests.js'的文件,其中包含以下内容:

import * as enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';

enzyme.configure({ adapter: new Adapter() });
Run Code Online (Sandbox Code Playgroud)

我通过以下方式运行:

npm测试

我收到了错误:

酶内部错误:酶需要配置适配器,但没有找到.要配置适配器,您应该调用Enzyme.configure({ > adapter: new Adapter() })

你知道什么可以解决这个问题吗?

reactjs enzyme create-react-app

33
推荐指数
6
解决办法
2万
查看次数

标签 统计

create-react-app ×1

enzyme ×1

reactjs ×1