jest: @jest-environment node not working in v22

Seeing this error message:

TypeError: environment.setup is not a function
      
      at node_modules/jest-runner/build/run_test.js:112:23

When using this syntax at the top of the test file (worked in v21):

/**
 * @jest-environment node
 */

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 26 (9 by maintainers)

Commits related to this issue

Most upvoted comments

If you have both react-scripts and jest in your package.json, delete jest from it. Then delete package-lock.json, yarn.lock and node_modules. Then run npm install (or yarn if you use it).

@dougbacelar

It is not supported to update Jest manually in a CRA project. If you have both jest and react-scripts in your package.json you should delete anything with jest in package.json and wait until we update react-scripts ourselves πŸ˜‰

Had the same problem and solved it by downgrading Jest on my package.json from

"jest": "22.0.4", to "jest": "20.0.4",

I just changed the version and ran yarn install

Before the fix my output for the command npm ls jest-environment-node was:

β”œβ”€β”¬ jest@22.0.4
β”‚ └─┬ jest-cli@22.0.4
β”‚   └─┬ jest-config@22.0.4
β”‚     └── jest-environment-node@22.0.4 
└─┬ react-scripts@1.0.17
  └─┬ jest@20.0.4
    └─┬ jest-cli@20.0.4
      └─┬ jest-config@20.0.4
        └── jest-environment-node@20.0.3

After the fix:

└─┬ jest@20.0.4
  └─┬ jest-cli@20.0.4
    └─┬ jest-config@20.0.4
      └── jest-environment-node@20.0.3 

Would someone please explain the fix for this? I was not clear after reading through this problem.

I setup my app with

Create React App

When I run npm test I get errors.

First it was jest-cli that I needed. Once I installed jest-cli I started seeing this error,

TypeError: environment.setup is not a function

When I run,

npm ls jest-environment-node

β”œβ”€β”¬ jest-config@22.0.4
β”‚ β”‚ └── jest-environment-node@22.0.4
β”‚ └─┬ jest-runner@22.0.4
β”‚   └─┬ jest-config@22.0.4
β”‚     └── jest-environment-node@22.0.4
└─┬ react-scripts@1.0.17
  └─┬ jest@20.0.4
    └─┬ jest-cli@20.0.4
      └─┬ jest-config@20.0.4
        └── jest-environment-node@20.0.3

This is my .babelrc

{
  "plugins": [
    "transform-flow-strip-types",
  ],
  "presets": [
    "env",
    "react"
  ]
}

This is my package.json


  "scripts": {
    "start": "react-scripts start",
    "flow": "flow",
    "lint": "eslint .",
    "prettier": "find . -name node_modules -prune -or -name '*.js' -print | xargs prettier --write",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "dependencies": {
    "bootstrap": "^3.3.7",
    "lodash": "^4.17.4",
    "react": "^16.1.0",
    "react-bootstrap": "^0.31.5",
    "react-dom": "^16.1.0",
    "react-fontawesome": "^1.6.1",
    "react-google-maps": "^9.4.3",
    "react-redux": "^5.0.6",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17",
    "react-tabs": "^2.1.1",
    "recharts": "^1.0.0-beta.7",
    "redux": "^3.7.2"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-eslint": "^8.0.2",
    "babel-plugin-transform-flow-strip-types": "^6.22.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "eslint": "^4.11.0",
    "eslint-config-fb-strict": "^21.2.0",
    "eslint-config-fbjs": "^2.0.0",
    "eslint-plugin-babel": "^4.1.2",
    "eslint-plugin-flowtype": "^2.39.1",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-prettier": "^2.3.1",
    "eslint-plugin-react": "^7.4.0",
    "flow-bin": "^0.59.0",
    "jest-cli": "^22.0.4",
    "prettier": "^1.8.2",
    "redux-devtools": "^3.4.1"
  }
}

If you use create-react-app you can’t upgrade jest without them doing it as well

have you upgraded the dependency on the env as well?

npm ls jest-environment-node

--use-npm image

If you have both react-scripts and jest in your package.json, delete jest from it. Then delete package-lock.json, yarn.lock and node_modules. Then run npm install (or yarn if you use it).

@gaearon You are the real MVP, thanks! I accidentally installed those packages during setup and this saved me.

I update jest-preset-angular@latest (which is version 5.0.0) and this solved the problem for me.

@gaearon, that didn’t fix what I’m running into with a new CRA app (TypeError: environment.setup is not a function)

It turns out that --env=jsdom was my problem…back to bliss.

It seems like at least in part the problem is on the Jest side. I created an issue: https://github.com/facebook/jest/issues/5294.

If you are trying @gaearon’s fix and have jest-cli then delete that as well.

I think the same checks should be done for CRNA as well.

We’ll start doing preflight checks in future CRA versions to fail fast with clear instructions when this happens. https://github.com/facebookincubator/create-react-app/pull/3771