本文介绍了酶期望配置适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过create-react-app创建了一个新的React应用程序,我想向在该应用程序中创建的名为 MessageBox的组件编写单元测试。这是我写的单元测试:

I created a new React application by create-react-app and I wanted to write a unit test to a component named "MessageBox" that I created in the application. This is the unit test that I wrote:

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的文件,其内容为:

I also added a file under 'src' folder named 'setupTests.js' with the content:

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

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

我通过以下方式运行它:

I ran it by:

,我得到了错误:

酶内部错误:酶希望配置适配器,但
找不到。要配置适配器,应调用 Enzyme.configure({> adapter:new Adapter()})

您知道什么可以解决此问题吗?

Do you know what can solve this problem?

推荐答案

将其添加到测试用例文件中

Add it to your test case file.

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

configure({adapter: new Adapter()});
test('message box', ()=> {
     ...
})

这篇关于酶期望配置适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:10
查看更多