Which Enzyme adapter works with React 17?

Solution 1:

What enzyme adapter is there for React 17?


If you have React version 17, you can use this unofficial adapter for React 17 for enzyme.

// src/setupTests.js
import { configure } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';

configure({ adapter: new Adapter() });

See this GitHub issue for more information on the unofficial adapter.

Solution 2:

I tried with unofficial adapter '@wojtekmaj/enzyme-adapter-react-17' and it works fine. Thanks @super Jade.

  1. clear the npm cache (clearing this may make npm slower, but give it a try).
  • npm cache verify and then npm cache clean --force
  1. remove your
  • node_modules and package-lock.json
  1. update npm dependencies in your package.json with "@testing-library/react": "^11.2.5","@wojtekmaj/enzyme-adapter-react-17": "^0.4.1","react": "^17.0.1","react-dom": "^17.0.1",
  2. npm i --legacy-peer-deps
  3. as @wojtekmaj/enzyme-adapter-react-17 mentioned making a change in the test file.
  • import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
  • configure ({ adapter: new Adapter() });

Solution 3:

add this code in setupTests.js file

// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
import Enzyme from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';

Enzyme.configure({ adapter: new Adapter() });

enter image description here