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?
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
to react-scripts test --env=jsdom
, as it was.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With