reactjs - Enzyme 需要配置适配器

标签 reactjs enzyme create-react-app

我通过 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/

相关文章:

javascript - react 令人讨厌的错误,我无法弄清楚

reactjs - pusher .bind 方法中的过时 react 状态

javascript - 如何将 Enzyme Shallow 与 Jest 快照一起使用

reactjs - React Enzyme 找到第二个(或第 n 个)节点

javascript - 使用弹出的 create-react-app 创建 SVG Sprite 组件

javascript - 如何重构检查图像(用作 css 背景法师)是否存在以返回 bool 值的函数?

javascript - 状态的功能被另一个状态破坏了?

reactjs - 类型错误 : Cannot assign to read only property 'x' of object '#<Object>' React/JEST

reactjs - 由于 React SPA 中的 Hasbangs,无法在刷新时访问页面

reactjs - 在 create-react-app 中高效导入多个相似的 svg 源?