Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setupTests.js not loading automatically in CRA react app

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?

like image 880
Travis Avatar asked Dec 05 '18 23:12

Travis


People also ask

What is the use of setupTests JS in react?

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.

Can I delete setupTests JS?

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.

Can I use enzyme with 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.

What is ENV D TS react?

The react-app-env. d. ts references the types of react-scripts , and helps with things like allowing for SVG imports.


1 Answers

Does it work when you add the following to your package.json

"scripts": {
  ...
  "test": "react-scripts test",
  ...

}
like image 134
DiverseAndRemote.com Avatar answered Sep 25 '22 12:09

DiverseAndRemote.com