【问题标题】:enzyme: TypeError: Adapter is not a constructor酶:TypeError:适配器不是构造函数
【发布时间】:2018-09-24 06:02:45
【问题描述】:

您好,我正在尝试使用酶测试反应应用程序,但它会引发错误 TypeError: Adapter is not a constructor ,任何想法

这是我的测试文件

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 不是构造函数

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    我不认为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'],
    

    【讨论】:

    • 我正在使用以下代码并得到相同的错误。从“酶”中导入酶从“酶适配器-preact-pure”中导入适配器; Enzyme.configure({ adapter: new Adapter() });
    【解决方案2】:

    对于打字稿:

    import { configure } from 'enzyme';
    import * as ReactSixteenAdapter from 'enzyme-adapter-react-16';
    const adapter = ReactSixteenAdapter as any;
    configure({ adapter: new adapter.default() });
    

    【讨论】:

    • 谢谢我一直在找这个
    【解决方案3】:

    你需要像这样使用导入:

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

    这样:(import * as Adapter from ...) 返回消息“TypeError: Adapter is not a constructor.”

    【讨论】:

      【解决方案4】:

      如果您使用启用了 esModuleInterop 选项的 Typescript,您将需要这个:

      import { configure} from 'enzyme';
      import * as ReactSixteenAdapter from 'enzyme-adapter-react-16';
      configure({ adapter: new (ReactSixteenAdapter as any)() });
      

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 2018-06-27
        • 2016-04-16
        • 2021-11-26
        • 2019-10-27
        • 2018-07-18
        • 2016-12-20
        相关资源
        最近更新 更多