14

I have a problem calling enzym's mount function. It says:

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() })`
      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-15';

My setupTests.js file looks like this:

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

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

and in package.json:

"jest": {
    "testEnvironment": "jest-environment-jsdom-global",
    "setupFiles": [
        "<rootDir>/jestConfigs/setupTests.js"
    ],
    "moduleNameMapper": {
        "^.+\\.(css|scss)$": "<rootDir>/jestConfigs/CSSStub.js",
        "^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jestConfigs/fileStub.js",
        "/common-mms/(.+)": "<rootDir>/App/common-mms/$1"
    }
},

But the problem is - I am using react-16 and enzyme-adapter-react-15 is irrelevant for me. Even if I add enzyme-adapter-react-15 just in case - the error still remains.


UPDATE: If I copy content of setupTests.js into beginning of each test file - all works fine! And if I put console.log(1) into setupTests - it actually prints! Meaning that the file is actually improted on jest initialization.

kurumkan
  • 2,635
  • 3
  • 31
  • 55

2 Answers2

5

You have to import setupTests.js in your file where you're writing your test cases. This is already answered in this link

Vikram Thakur
  • 2,329
  • 1
  • 11
  • 16
1

You shouldn't be copying the adapter setup in all your tests. Indeed this has been answered before: Enzyme expects an adapter to be configured.

In short, the solution depends on whether you have used create-react-app to set up the project and have ejected the app or not.

For a non-ejected create-react-app project you only need to add the setupTests.js file you mention under src/. For Typescript users this should be src/setupTests.ts.

If you have ejected from create-react-app or have setup the project without it, you need to add to your jest config this line (wherever that is): "setupFilesAfterEnv": ["<rootDir>/src/setupTests.js"]

gkri
  • 1,627
  • 3
  • 18
  • 27