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?


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', ()=> {
     ...
})

Also, if you don't want to import your setupTests.js file into every test file, you can place it in your package.json folder:

  "jest": {
     "setupTestFrameworkScriptFile": "./test/setupTests.js" }

Update:

Note: setupTestFrameworkScriptFile is deprecated in favor of setupFilesAfterEnv.

https://jestjs.io/docs/en/configuration


The file 'setupTests' has to be imported to the test file:

import MessageBox from "../MessageBox";
import { shallow } from 'enzyme';
import React from 'react';
import "../setupTests"

test('message box', ()=> {
     ...
})

As Priyank said, if you are using Create React App, it will pick up the configuration from src/setupTests.js.

Add:

import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

configure({ adapter: new Adapter() })

A lot of answers are saying import setupTests.js into your test file. Or configure enzyme adapter in each test file. Which does solve the immediate problem.

But long term, if you add a jest.config.js file to the project root. You can configure it to run a setup file on launch.

jest.config.js

module.exports = {
  setupTestFrameworkScriptFile: "<rootDir>/src/setupTests.ts"
}

This tells Jest to run setupTest.ts every time it's launched.

This way if you need to add polyfills or add global mock like localstorage, you can add it to your setupTests file and its configured everywhere.

The Enzyme docs don't cover integration with Jest, so it is confusing to fuse these two together.