我通过 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 test
我得到了错误:
Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none. To configure an adapter, you should call
Enzyme.configure({ > adapter: new Adapter() })
你知道什么可以解决这个问题吗?
最佳答案
将其添加到您的测试用例文件中。
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { shallow, configure } from 'enzyme';
configure({adapter: new Adapter()});
test('message box', ()=> {
...
})
关于reactjs - Enzyme 需要配置适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50222545/