I was trying to implement the example shown in Jest website: Getting started with Jest.
While running npm test
on I was getting the following error:
Add
"jest": {
"testEnvironment": "node"
}
to your package.json file, your package.json would look like this:
{
"name": "jest-demo-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.0.17"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject"
},
"devDependencies": {
"jest": "^22.0.4"
},
"jest": {
"testEnvironment": "node"
}
}
UPDATE
for the people using create app and having this problem, there are two solutions.
1- update test
command (or add new one) in package.json to jest
instead of react-scripts test
, because react-scripts test
will not allow jest options in package.json as @Xinyang_Li stated in comments.
2-if you don't want to change test command in package.json, and use default create-react-app test; remove jest options from packge.json (if exists)
"jest": {
"testEnvironment": "node"
}
then remove node_modules and then run npm install to install them all again, and after that your tests should run normally.