Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@jest-environment node not working in v22 #5119

Closed
evan-scott-zocdoc opened this issue Dec 18, 2017 · 26 comments
Closed

@jest-environment node not working in v22 #5119

evan-scott-zocdoc opened this issue Dec 18, 2017 · 26 comments
Labels

Comments

@evan-scott-zocdoc
Copy link

evan-scott-zocdoc commented Dec 18, 2017

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
 */
@SimenB
Copy link
Member

SimenB commented Dec 18, 2017

have you upgraded the dependency on the env as well?

npm ls jest-environment-node

@evan-scott-zocdoc
Copy link
Author

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

├─┬ jest@22.0.0
│ └─┬ jest-cli@22.0.0
│   ├─┬ jest-config@22.0.0
│   │ └── jest-environment-node@22.0.0 
│   ├─┬ jest-runner@22.0.0
│   │ └─┬ jest-config@22.0.0
│   │   └── jest-environment-node@22.0.0 
│   └─┬ jest-runtime@22.0.0
│     └─┬ jest-config@22.0.0
│       └── jest-environment-node@22.0.0 
└─┬ ts-jest@21.2.4
  └─┬ jest-config@21.2.1
    └── jest-environment-node@21.2.1 

They haven't upgraded yet.

@evan-scott-zocdoc
Copy link
Author

Thanks @SimenB

@SimenB
Copy link
Member

SimenB commented Dec 18, 2017

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

@wuno
Copy link

wuno commented Dec 29, 2017

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

@dougbacelar
Copy link

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 

@SimenB
Copy link
Member

SimenB commented Dec 30, 2017

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

@Chowarmaan
Copy link

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

@gaearon
Copy link
Contributor

gaearon commented Jan 8, 2018

@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 😉

@dougbacelar
Copy link

@gaearon

Cheers! Learning everyday 🙂

@gaearon
Copy link
Contributor

gaearon commented Jan 8, 2018

No worries!

@strizzwald
Copy link

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?

@gaearon
Copy link
Contributor

gaearon commented Jan 12, 2018

It seems like at least in part the problem is on the Jest side.
I created an issue: #5294.

@gaearon
Copy link
Contributor

gaearon commented Jan 13, 2018

We'll start doing preflight checks in future CRA versions to fail fast with clear instructions when this happens.
facebook/create-react-app#3771

@strizzwald
Copy link

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

@thiennhank9
Copy link

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

@gaearon
Copy link
Contributor

gaearon commented Jan 15, 2018

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

@Merovex
Copy link

Merovex commented Mar 14, 2018

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

@corysimmons
Copy link

corysimmons commented Mar 19, 2018

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 #5119 (comment)), resolves fine with Yarn.

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

@SimenB
Copy link
Member

SimenB commented Mar 19, 2018

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;

@corysimmons
Copy link

--use-npm
image

carpeliam added a commit to carpeliam/boston2delhi that referenced this issue Mar 27, 2018
- this avoids the following documented error jestjs/jest#5119
- this also removes `node_modules/.bin` which is automatically added to the path
@SSTPIERRE2
Copy link

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.

@DanS
Copy link

DanS commented May 17, 2018

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

@imtmh
Copy link

imtmh commented Jun 1, 2018

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.

@thomasmost
Copy link

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

elsaywang pushed a commit to elsaywang/destination that referenced this issue Mar 5, 2019
Jest is included in react-scripts. This was causing a problem
because that version was different than the one installed
at the top level of package.json.

For more details on the issue and solution, see this issue:
jestjs/jest#5119 (comment)
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests