Troubleshooting Jest with React: environment.setup is not a function

Dana Scheider (he/they)
1 min readMay 22, 2018

--

This is a short post about how I fixed a Jest error with my React app. This error affected two things:

  • Jest test runs failed with the error TypeError: environment.setup is not a function.
  • Travis CI passed despite the failure, probably due to npm test exiting with 0.

Initial Configuration

This was our package.json file:

This was our src/App.test.js file:

And this was our .travis.yml file:

The Solution

The solution turned out to be really simple. First, I deleted "jest":"^22.4.4", and "jest-cli":"^22.4.4", from the package.json file. Next, I ran rm -rf node_modules from within the project directory before running npm install again. This way, all the Jest scripts were included through react-scripts instead of being installed separately.

Voilà! The errors stopped — and when I added a failing assertion to the test (I used expect(false).toBeTruthy();), the test and the Travis build both failed.

--

--