I have read that src/setupTests.js is supposed to load before every test but I still get every test failing with the error:
"Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none."
this is my src/setupTests.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'whatwg-fetch';
Enzyme.configure({ adapter: new Adapter() });
Here is my package.json:
"devDependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"coveralls": "^3.0.2",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0",
"enzyme-to-json": "^3.3.4",
"jest": "^23.6.0",
"react-test-renderer": "^16.5.2",
"redux-mock-store": "^1.5.3",
"regenerator-runtime": "^0.12.1",
"whatwg-fetch": "^3.0.0"
}
I am running a "test" script via npm run test
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:updateSnapshots": "jest --updateSnapshot"
}
I start every test file with some form of the following:
import React from 'react';
import { shallow } from 'enzyme';
I am on React 16 with react-scripts 2.1.1
Am I doing anything wrong that anyone can see?
setupTests. Like Jest and React Testing Library, Jest-DOM is installed with create-react-app. The setupTests. js file is importing the library into our app and giving us access to the matchers. The matcher that was used in our example test was the toBeInTheDocument() method.
Testing - setupTest. test. js is a basic test file. You can also safely delete both of these files if you are not experienced in testing.
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.
The react-app-env. d. ts references the types of react-scripts , and helps with things like allowing for SVG imports.
Does it work when you add the following to your package.json
"scripts": {
...
"test": "react-scripts test",
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With