reactjs - enzyme :类型错误:适配器不是构造函数

标签 reactjs jestjs enzyme

嗨,我试图用 enzyme 测试 react 应用程序,但它抛出一个错误TypeError:适配器不是构造函数,任何想法

这是我的测试文件

import ProductRow from '../product_row';
import React from 'react';
// import { mount } from 'enzyme';
import * as enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';
enzyme.configure({ adapter: new Adapter() });

test('TodoComponent renders the text inside it', () => {
  const wrapper = enzyme.mount(
    <ProductRow  item={{}} quickView={[]}
      productPage={''}
      count={0}
      numberOfColumns={0}
      title={'title'}
      taxonomies={{}}
      excerpt={'excerpt'}
    />
  );
});

TypeError: Adapter is not a constructor

最佳答案

我认为在导入具有默认导出的模块时 import * 无法按预期工作,这应该可以工作:

import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

Enzyme.configure({ adapter: new Adapter() })
顺便说一句。您可以将上述内容放入一个文件中,并在 Jest 设置中引用它,这样您就不必将其添加到每个测试中:

setupFiles: ['<rootDir>/tools/jest/setup-react-adapter.js'],

关于reactjs - enzyme :类型错误:适配器不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49824423/

相关文章:

javascript - 如何使用 vue-test-utils 和 jest 在单元测试期间模拟 Vue Mixins?

reactjs - 如何使用 Jest 和 Enzyme 对 useEffect cleanUp 返回函数进行单元测试

javascript - 如何使用 HOC 编写适当的 Jest 测试?

eclipse - 如何在 eclipse 中将 ReactJs 与 Spring MVC 集成

node.js - 如何使用nodejs api对react.js应用程序的azure广告帐户进行身份验证?

javascript - 如何从客户端发送文件到http Node 服务器

javascript - Jest messageParent 只能在工作人员内部使用

reactjs - 在 Jest 测试中,它说 withRouter () 不是函数

javascript - Redux 容器单元测试 - 无法访问包装组件的 Prop

javascript - 如何在 React 中的 GraphQL 突变组件中包含的按钮上有多个 onClick 事件