Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enzyme: TypeError: Adapter is not a constructor

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

like image 673
Gopinath Kaliappan Avatar asked Apr 13 '18 20:04

Gopinath Kaliappan


3 Answers

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'],
like image 64
Andreas Köberle Avatar answered Sep 18 '22 18:09

Andreas Köberle


For TypeScript:

import { configure } from 'enzyme';
import * as ReactSixteenAdapter from 'enzyme-adapter-react-16';
const adapter = ReactSixteenAdapter as any;
configure({ adapter: new adapter.default() });
like image 10
user3540397 Avatar answered Sep 21 '22 18:09

user3540397


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."

like image 8
Filipe Avatar answered Sep 21 '22 18:09

Filipe