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:
FAIL src/sum.test.js
● Test suite failed to run
TypeError: environment.setup is not a function
at node_modules/jest-runner/build/run_test.js:112:23
sum.js
:
function sum(a, b){
return a+b;
}
module.exports = sum;
sum.test.js
:
const sum = require('./sum');
test('adding sum function', () => {
expect(sum(234,4)).toBe(238);
})
sum.js
and sum.test.js
are an exact copy of the example shown in Getting started with Jest.
package.json
:
{
"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"
}
}
So, how can I get rid of TypeError: environment.setup is not a function
error?
Based on this github issue: @jest-environment node not working in v22,I updated the
sum.test.js
as:Now I got rid of theTypeError: environment.setup is not a function
.Output:
Updated
Reading several answers, today I recreated the scenario.
Step 1: I created a new project using
create-react-app
.The command:
create-react-app demo-app
package.json
file:This file shows that
jest
is not included in the dependencies.Step 2: I included
sum.js
andsum.test.js
files insrc
folder as shown in the official getting started guide.sum.js
:sum.test.js
:Step 3: I ran
yarn test
command without includingjest
inpackage.json
file. So, this test command usesreact-scripts test --env=jsdom
as shown inpackage.json
.Output of the test:
Step 4: I updated the
package.json
file to usejest
as test script:Again I ran
yarn test
and the output is:Too Long; Did not Read (TL;DR)
jest
usingyarn add --dev jest
ornpm install --save-dev jest
if you usecreate-react-app
command.jest
is included innode-modules
of the project if you usecreate-react-app
.From official Jest documentation:
package.json
file { look at this issue }. Keep the default"test": "react-scripts test --env=jsdom",
in that.You should downgrade installed
jest
tov20.0.4
npm install -D jest@20.0.4
Just remove
jest
from your dev dependencies. To do so, run the following command:Jest is included with react-scripts. The error is due to a conflict that arose when you installed Jest in a project started with react-scripts. The "Getting started with Jest" guide expects a 'clean' project.
Simply remove Jest from your (dev)dependencies and it should work.
If you want to use Jest for testing React components (which you probably do), you need to modify your test script in
package.json
toreact-scripts test --env=jsdom
, as it was.Add
to your package.json file, your package.json would look like this:
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 tojest
instead ofreact-scripts test
, becausereact-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)
then remove node_modules and then run npm install to install them all again, and after that your tests should run normally.
This is an older issue but I was facing the same problem. We have
react-scripts
package and it uses Jest version20.x.x
which has problems withcoverageTreshold
. I couldn't use newerreact-scripts
version and the only thing helped was to use yarn resolutions, read more here https://yarnpkg.com/lang/en/docs/selective-version-resolutions/.