我通过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:
npm测试
npm test
,我得到了错误:
酶内部错误:酶希望配置适配器,但找不到。要配置适配器,应调用 Enzyme.configure({> adapter:new Adapter()})
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() })
您知道什么可以解决此问题吗?
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', ()=> { ... })更多推荐
酶期望配置适配器
发布评论