12

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?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Travis
  • 388
  • 1
  • 5
  • 11
  • 1
    Did you try restarting the test runner? This is pretty much identical to my working set up. – David Mar 22 '19 at 17:55
  • @DeveloperDavo I have deleted my node_modules folder and `npm install`ed it multiple times. Does that count as restarting my test runner? What exactly is the test runner in this case? – Travis Mar 23 '19 at 18:29
  • Indirectly :). Rerunning `npm run test` (or `npm test`) would do the trick, but I now see the question has already been answered. – David Mar 24 '19 at 07:49

5 Answers5

6

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

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

}
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
  • This worked. Any idea why? I guess the "Jest" command bypasses the CRA defaults? – Travis Mar 23 '19 at 19:31
  • I believe it is because `react-scripts test` creates a Jest config that would be bypassed by running `jest` instead. Here is the [source code](https://github.com/facebook/create-react-app/blob/3be35763044c690458e806c255e100336de0d9a1/packages/react-scripts/scripts/test.js) – David Mar 24 '19 at 07:53
3

Update package.json.

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test setupFile ./src/setupTests.js",
    "eject": "react-scripts eject"
  }
Gaurav Kandpal
  • 1,250
  • 2
  • 15
  • 33
2

I ended up here with the same error but another cause:

I had a typo in the file name:

  • src/setupTest.js instead src/setupTest*s*.js

Then the default CRA (creat-react-app) package.json command loads the Adapter into your test:

  • "test": "react-scripts test"
hb0
  • 3,350
  • 3
  • 30
  • 48
1

If you made the setupTests.js file while in watch mode in create-react-app's npm test script, then you need to quit watch mode and restart it for it to begin working.

brennvo
  • 89
  • 1
  • 7
0

It should work with this configuration. As same configuration works in my app. Try putting import in test file.

import "../setupTests"

Sometimes this works.

Anil Kumar
  • 2,223
  • 2
  • 17
  • 22
  • I have put a "setupTestFrameworkScriptFile" override in my package.json before and that works but it breaks the vs code debugger because it shouldn't be necessary. – Travis Dec 06 '18 at 01:06