纱线测试失败 - 网络请求失败 [英] Yarn test failing - Network request failed
问题描述
我使用 create-react-app
创建了一个 react 项目,但是当我运行 yarn test
以查看初始测试是否通过时
I created a react project using create-react-app
however when ran yarn test
to see if the initial tests pass I get
TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (/home/ubuntu/wonderland/hackernews/node_modules/whatwg-fetch/fetch.js:436:16)
at XMLHttpRequest.callback.(anonymous function) (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:289:32)
at invokeEventListeners (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:219:27)
at invokeInlineListeners (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:166:7)
at EventTargetImpl._dispatch (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:122:7)
at EventTargetImpl.dispatchEvent (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
at XMLHttpRequest.dispatchEvent (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:61:35)
at XMLHttpRequest.abort (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:405:16)
at Object.abort (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:315:13)
at RequestManager.close (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:146:21)
at Window.close (/home/ubuntu/wonderland/hackernews/node_modules/jsdom/lib/jsdom/browser/Window.js:362:29)
at JSDOMEnvironment.dispose (/home/ubuntu/wonderland/hackernews/node_modules/jest-environment-jsdom/build/index.js:44:19)
at Promise.resolve.then (/home/ubuntu/wonderland/hackernews/node_modules/jest-cli/build/runTest.js:102:17)
我在 Cloud9 编辑器中工作,使用 ssh 连接到我在 AWS 中托管的开发服务器 (Ubuntu).测试在我的本地机器(Ubuntu)上运行良好.
I'm working in Cloud9 editor using ssh connected to my dev server (Ubuntu) hosted in AWS. Tests are working fine in my local machine (Ubuntu).
感谢您的帮助!
推荐答案
我已经解决了我的问题.我正在关注一本名为The Road to Learn React"的书,我认为导致我出错的原因是我从外部 API fetch
数据的部分.这本书忘记包含一个用于 fetch 的导入,就是这个:
I already solve my problem. I'm following a book entitled "The Road to Learn React" and I think that the one causing my error is the part where I fetch
data from an external API. The book forgot to include an import for fetch which is this one:
import fetch from 'isomorphic-fetch'
然后,它像魅力一样解决了我的问题.
Then, it solves my problem like a charm.
这篇关于纱线测试失败 - 网络请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!