TypeError:environment.setup不是React Testing中的函数 [英] TypeError: environment.setup is not a function in React Testing
问题描述
我试图实现Jest网站上显示的示例:入门与Jest 。
I was trying to implement the example shown in Jest website: Getting started with Jest.
在运行 npm test
时,我收到以下错误:
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
和总和.test.js
是 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"
}
}
那么,如何摆脱 TypeError:environment.setup不是函数
e rror?
So, how can I get rid of TypeError: environment.setup is not a function
error?
推荐答案
Jest包含在react-scripts中。该错误是由于在使用react-scripts启动的项目中安装Jest时出现的冲突。 Jest入门指南需要一个干净的项目。
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.
只需从您的(dev)依赖项中删除Jest即可。
Simply remove Jest from your (dev)dependencies and it should work.
如果你想使用Jest来测试React组件(你可能会这样做),你需要在 package.json <中修改你的测试脚本/ code>到
react-scripts test --env = jsdom
,就像它一样。
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.
这篇关于TypeError:environment.setup不是React Testing中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!