create-react-app: Error on tests when Enzyme is added
Is this a bug report?
Yes
Can you also reproduce the problem with npm 4.x?
Yes;
Which terms did you search for in User Guide?
I searched about testing, about enzyme and on the troubleshooting page as well.
Environment
node -v
: v6.11.3npm -v
: 4.6.1yarn --version
(if you use Yarn): 1.1.0npm ls react-scripts
(if you haven’t ejected): react-scripts@1.0.14
Then, specify:
- Operating system: Windows
Steps to Reproduce
- npm install -g create-react-app
- create-react-app test-jest-enzyme
- cd test-jest-enzyme
- Change file src/App.test.js to be:
import React from 'react';
import App from './App';
import {shallow} from "enzyme";
it('renders without crashing', () => {
shallow(<App />);
});
- npm test
Expected Behavior
The tests would pass and no exception would be thrown.
Actual Behavior
Will give:
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';
To find out more about this, see http://airbnb.io/enzyme/docs/installation/index.html
at validateAdapter (node_modules/enzyme/build/validateAdapter.js:14:11)
at Object.<anonymous>.it (src/App.test.js:6:25)
Reproducible Demo
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 14
- Comments: 15 (6 by maintainers)
I can confirm that putting:
In the
src/setupTests.js
file solves the issue.ok, think I fixed it, not sure if it’s the correct way but it works for ejected CRA by adding to
package.json
jest property :Yes, Enzyme 3 has some breaking changes. Please read their announcement:
https://github.com/airbnb/enzyme#upgrading-from-enzyme-2x-or-react--16
If you use Create React App, you can put the
configure()
call intosrc/setupTests.js
.my
package.json
’sjest
section already had an entry forsetupFiles
:and adding my setupTests entry into this array didn’t work - it looks like the file wasn’t run. I had to add the following as a separate entry at the same level inside the
jest
section:"setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js",
and then all was unicorns and rainbows.
This path (
react-test-renderer/lib/
) doesn’t exist in 16. This indicates you are using an old version ofreact-test-renderer
. Please update all packages together to 16.