Jest: @jest-environment node not working in v22

Created on 18 Dec 2017  ยท  25Comments  ยท  Source: facebook/jest

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
 */
Wontfix

Most helpful comment

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).

All 25 comments

have you upgraded the dependency on the env as well?

npm ls jest-environment-node

Ah you know what, it's ts-jest that's the issue.

โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ”œโ”€โ”ฌ [email protected]
โ”‚   โ”‚ โ””โ”€โ”€ [email protected] 
โ”‚   โ”œโ”€โ”ฌ [email protected]
โ”‚   โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ”‚   โ””โ”€โ”€ [email protected] 
โ”‚   โ””โ”€โ”ฌ [email protected]
โ”‚     โ””โ”€โ”ฌ [email protected]
โ”‚       โ””โ”€โ”€ [email protected] 
โ””โ”€โ”ฌ [email protected]
  โ””โ”€โ”ฌ [email protected]
    โ””โ”€โ”€ [email protected] 

They haven't upgraded yet.

Thanks @SimenB

I don't like that jest-config depends on jest-environment-node :( (#4992 for explanation)

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

โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ””โ”€โ”€ [email protected]
โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ””โ”€โ”ฌ [email protected]
โ”‚     โ””โ”€โ”€ [email protected]
โ””โ”€โ”ฌ [email protected]
  โ””โ”€โ”ฌ [email protected]
    โ””โ”€โ”ฌ [email protected]
      โ””โ”€โ”ฌ [email protected]
        โ””โ”€โ”€ [email protected]

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"
  }
}

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:

โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ””โ”€โ”ฌ [email protected]
โ”‚     โ””โ”€โ”€ [email protected] 
โ””โ”€โ”ฌ [email protected]
  โ””โ”€โ”ฌ [email protected]
    โ””โ”€โ”ฌ [email protected]
      โ””โ”€โ”ฌ [email protected]
        โ””โ”€โ”€ [email protected]

After the fix:

โ””โ”€โ”ฌ [email protected]
  โ””โ”€โ”ฌ [email protected]
    โ””โ”€โ”ฌ [email protected]
      โ””โ”€โ”€ [email protected] 

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

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

@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 ๐Ÿ˜‰

@gaearon

Cheers! Learning everyday ๐Ÿ™‚

No worries!

I also have this issue on create-react-native-app(CRNA) here is my package.json

{
  "name": "sportomatic-mobile",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "flow-bin": "^0.56.0",
    "jest-expo": "23.0.0",
    "react-native-scripts": "1.8.1",
    "react-test-renderer": "16.0.0"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "flow": "flow",
    "test": "node node_modules/jest/bin/jest.js --watch"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "@expo/vector-icons": "^6.2.2",
    "expo": "^23.0.4",
    "firebase": "^4.8.1",
    "jest-cli": "^22.0.5",
    "material-ui": "^0.20.0",
    "native-base": "^2.3.5",
    "react": "16.0.0",
    "react-native": "0.50.3",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0",
    "reselect": "^3.0.1"
  }
}

I do not have react-scripts. Should I then just update manually?

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.

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

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

Sorry, but I meet the same problem. Anyone know how to fix it?

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, 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.

npx create-react-app foo; cd foo; npm i; npm test throws "You gotta install jest-cli" err, because npm i does some crap to jest.

npx create-react-app foo; cd foo; yarn test works fine.

I think it's a problem with Jest's package.json not resolving jest-cli via npm (re @gaearon 's comment above https://github.com/facebook/jest/issues/5119#issuecomment-357346488), resolves fine with Yarn.

Protip: If you're using anything Facebook, just use Yarn and save yourself headaches.

create-react-app by default uses yarn, so if you want to use npm, just do npx create-react-app foo --use-npm; cd foo; npm test;

--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.

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

I faced this issue when I first ran yarn, followed by npm i.

I just removed node_modules folder (rm -rf node_modules) from project root and ran yarn
The issue got disappeared.

For anyone who might still be seeing this, make sure to update jest globally too -- and update any jest presets or extensions as well

Was this page helpful?
0 / 5 - 0 ratings