Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Usage with Enzyme, error: Enzyme expects an adapter #185

Closed
quantuminformation opened this issue Nov 6, 2017 · 16 comments
Closed

Usage with Enzyme, error: Enzyme expects an adapter #185

quantuminformation opened this issue Nov 6, 2017 · 16 comments

Comments

@quantuminformation
Copy link

I have the following setup

image

but get this 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() })`
          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';

I get the same error if I rename to setupTests.ts

@adepatie
Copy link

adepatie commented Nov 8, 2017

Experiencing the same issue. I've tried changing between "setupFiles" and "setupTestFrameworkScriptFile", using 'require()', 'import * as'. Same error.

@adepatie
Copy link

adepatie commented Nov 8, 2017

@quantuminformation Fixed my issue by using the answer found here:
https://stackoverflow.com/questions/46435558/could-not-find-declaration-file-for-enzyme-adapter-react-16

@smacpherson64
Copy link

Similar to @adepatie and the stackoverflow above,

I have a working setup with the following:

package.json

...
"enzyme": "^3.0.0",
"enzyme-adapter-react-16": "^1.0.2",
"@types/enzyme": "^3.1.0",
"react-scripts-ts": "2.8.0",
...

setupTests.ts:

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

@NirBenita
Copy link

NirBenita commented Nov 12, 2017

Thanks for the workaround people ❤️

I've been having similar problems. It looks like src/setupTests.js is not running before every test file (as it should). I work around the issue by adding:

import * as Adapter from 'enzyme-adapter-react-15';

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

to the test suite, which works fine for now since it's a small project with not many suites.

@smacpherson64
Copy link

Hey @NirBenita have you tried renaming that file to setupTests.ts ?

@wmonk
Copy link
Owner

wmonk commented Nov 13, 2017

Could someone submit a PR to get this working correctly?

@ianschmitz
Copy link
Contributor

src/setupTests.ts should be working correctly (is for me on 2.8.0)

@dbenchi
Copy link

dbenchi commented Nov 29, 2017

It does not work at all for me

"enzyme": "3.2.0",
"enzyme-adapter-react-16": "1.1.0",
"react-scripts-ts": "2.8.0",

@DorianGrey
Copy link
Collaborator

It does not work at all for me

@abenchi , would you mind elaborating this ? "It does not work" lacks precision.
If you follow these instructions (that's from a PR, but it has only some minor differences from the original version here), what exactly fails to work?

@DorianGrey
Copy link
Collaborator

Instructions on how to setup enzyme have been adopted to conform to the particular typescript requirements.
See here. Yes, it's somewhat hidden, but in the same position as in the original CRA.

@dbenchi
Copy link

dbenchi commented Dec 1, 2017

Indeed, it was my error, by doing the follwing to avoid the requestAnimationFrame warnings

"test": "react-scripts test --env=jsdom --setupTestFrameworkScriptFile=raf/polyfill",

I override the setupTests...
Thanks for your feedback

@arbtfnf
Copy link

arbtfnf commented May 1, 2018

This error occurs when you are using React16, and trying to install Enzyme@2 with "npm i --save-dev enzyme",
Instead use the following code to run Enzyme with React-16
yarn add enzyme@3.0.0 enzyme-adapter-react-16@1.0.0 raf@3.3.2

Here we have to use enzyme@3.0.0 to run it with react16, here we need to install adaptor also.
So after running this code, inside test folder, create a file( name it eg: setupTest.js ) to wire-up adaptor with enzyme.
In setupTest.js file copy/paste the following:

import Enzyme from 'enzyme';
import Adaptor from 'enzyme-adaptor-react-16';

Enzyme.configure({
adaptor: new Adaptor()
});

Now you need to add jest.config.json file on the root as it, this will tell to run raf then setupTests.js file
{ "setupFiles": [ "raf/polyfill", "<rootDir>/src/tests/setupTests.js" ] }

Update this in package.json file:
"test": "jest --config=jest.config.json",

@manoharreddyporeddy
Copy link

A simple solution is here:
enzymejs/enzyme#1265 (comment)

@quocdai92
Copy link

quocdai92 commented Jul 12, 2018

It is my sample used create-react-app

  1. I created an application by using create-react-app
  2. I install enzyme and enzyme-adapter-react-16 by yarn command.
  3. After that, I run command yarn eject
  4. I received a package.json file. Inside that, I modify the jest property (I added new line
    "<rootDir>/config/setupTests.js" in the "setupFiles" property.
    "jest": {
    ...
    "setupFiles": [
    "<rootDir>/config/polyfills.js",
    "<rootDir>/config/setupTests.js"
    ]
    ...
    }
  5. After all, I copy setupTests.js into folder config.
    ==>>> It worked!!!
    (The setupTests.js you can create with below contents )
    import Enzyme from 'enzyme';
    import Adapter from 'enzyme-adapter-react-16';

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

@xargr
Copy link

xargr commented Aug 26, 2018

@quocdai92 you save my day.... works for me

"jest": {
...
"setupFiles": [
"<rootDir>/config/polyfills.js",
"<rootDir>/src/setupTests.js"
]

and inside src folder I put setupTests.js

@Nytheneals
Copy link

Created project using create-react-app, added Enyzme but go this error

screen shot 2019-01-15 at 4 22 53 pm

setUpTest.js

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

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

found out that i had a typo changed file to setupTests.js and ran yarn test again

package.json

{
  "name": "react_version",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@reach/router": "^1.2.1",
    "react": "^16.7.0-alpha.2",
    "react-dom": "^16.7.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.2",
    "styled-components": "^4.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "enzyme": "^3.8.0",
    "enzyme-adapter-react-16": "^1.7.1",
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests