纱线测试失败 - 网络请求失败

IT技术 reactjs jestjs cloud9-ide create-react-app
2021-05-11 22:28:15

我创建了一个react项目,create-react-app但是在运行yarn test时查看初始测试是否通过我得到

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)上运行良好。

谢谢您的帮助!

1个回答

我已经解决了我的问题。我正在关注一本名为“The Road to Learn React”的书,我认为导致我出错的是我fetch从外部 API 获取数据的部分这本书忘记包含一个用于 fetch 的导入,它是这样的:

import fetch from 'isomorphic-fetch'

然后,它像魅力一样解决了我的问题。