Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enzyme expects an adapter to be configured

I created a new React application by create-react-app and I wanted to write a unit test to a component named "MessageBox" that I created in the application. This is the unit test that I wrote:

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}); }); 

I also added a file under 'src' folder named 'setupTests.js' with the content:

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

I ran it by:

npm test

and I got the error:

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() })

Do you know what can solve this problem?

like image 368
CrazySynthax Avatar asked May 07 '18 21:05

CrazySynthax


People also ask

How do I configure the enzyme adapter in React?

To configure an adapter, you should call `Enzyme. configure({ adapter: new Adapter() })` before using any of Enzyme's top level APIs, where `Adapter` is the adapter corresponding to the library currently being tested. For example: import Adapter from 'enzyme-adapter-react-16';

What is the use of adapter in enzyme?

The adapter abstracts away anything that changes based on the React version so the core enzyme code can stay the same. mount and shallow are both exported from enzyme .

What is enzyme adapter React 16?

Enzyme is a JavaScript Testing utility for React that makes it easier to test your React Components' output. You can also manipulate, traverse, and in some ways simulate runtime given the output. Enzyme's API is meant to be intuitive and flexible by mimicking jQuery's API for DOM manipulation and traversal.

Can I use enzyme with React 17?

Back in August 2020, React 17 Release Candidate came out. Shortly after, an issue has been raised in Enzyme repository to add support for React 17. Immediately after, @layershifter has opened a PR adding an official enzyme-adapter-react-17 .


1 Answers

Add it to your test case file.

import React from 'react'; import Adapter from 'enzyme-adapter-react-16'; import { shallow, configure } from 'enzyme';  configure({adapter: new Adapter()}); test('message box', ()=> {      ... }) 
like image 177
GAJESH PANIGRAHI Avatar answered Sep 19 '22 14:09

GAJESH PANIGRAHI