enzyme: TypeError: Adapter is not a constructor

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

Hi I was trying to test the react application With enzyme, But it throws an error TypeError: Adapter is not a constructor , Any Idea

This is my test file

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

回答1:

I don't think import * works as expected when importing a module with a default export, this should work:

import Enzyme from 'enzyme' import Adapter from 'enzyme-adapter-react-16'  Enzyme.configure({ adapter: new Adapter() }) 

BTW. you can put the above in a file and reference it in your Jest settings so you don't have to add this to every test:

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


回答2:

For TypeScript:

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


回答3:

You need to use the import like this:

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

This way: (import * as Adapter from ...) returns a message "TypeError: Adapter is not a constructor."



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!