酶期望配置适配器

编程入门 行业动态 更新时间:2024-10-24 12:33:42
本文介绍了酶期望配置适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通过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', ()=> { ... })

更多推荐

酶期望配置适配器

本文发布于:2023-10-16 10:28:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适配器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!