我使用create-react-app创建了一个简单的反应项目。刚创建项目后,纱线测试命令工作正常,然后我开始添加一些实际的反应代码,我最终在测试执行后得到了这个错误:
PASS src/test/App.test.js
✓ renders without crashing (28ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.729s, estimated 1s
Ran all test suites related to changed files.
Watch Usage
› Press a to run all tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-scripts/scripts/test.js:20
throw err;
^
TypeError: Cannot read property 'createEvent' of undefined
at Object.invokeGuardedCallbackDev (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:577:25)
at invokeGuardedCallback (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:436:27)
at renderRoot (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10386:7)
at performWorkOnRoot (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:11000:24)
at performWork (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10952:7)
at requestWork (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10861:7)
at scheduleWorkImpl (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10744:11)
at scheduleWork (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10706:12)
at Object.enqueueSetState (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:6204:7)
at App.Object.<anonymous>.Component.setState (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react/cjs/react.development.js:226:16)
at then.then.jsonData (/Users/mpegorar/Development/SpringApps/fluxy_frontend/src/components/App.js:25:9)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
测试实际上是执行的,如果我在里面放了一些错误,它就会改变,但是工具会退出,而通常它会保持运行并继续执行基于代码的测试。
我的代码中似乎导致此错误的行是此处描述的设置状态:
componentDidMount() {
const headers = new Headers()
headers.append('Content-Type', 'application/hal+json')
fetch(`${url}/listEnvironmentResources`, { headers }).then(
function(response){
return response.json();
}
).then(jsonData => {
this.setState({environmentRecords: jsonData._embedded.environmentRecordResources});
});
}
之前由于this error,测试无法正常工作,修正了已接受的答案。
我发现错误的唯一参考是this,但它没有解决我的问题。
任何想法我该如何解决这个问题?看起来像缺少一些依赖但无法找到哪一个。