react jest单元测试出现错误connect ECONNREFUSED 127.0.0.1:80

时间:2025-02-24 14:41:54

背景:最近负责一个公司的开源项目,由于要求代码覆盖率达到95%,而接受项目的时候是96%,意味之我有1%的空间。。。
问题描述:
1.在写了一些单元测试的时候,发现在跑test命令的时候出现了下图的错误

console.error node_modules/@testing-library/react/dist/act-compat.js:52
      Error: Error: connect ECONNREFUSED 127.0.0.1:80
          at Object.dispatchError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
          at Request.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)
          at Request.emit (events.js:326:22)
          at Request.onRequestError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/request/request.js:877:8)
          at ClientRequestOverride.emit (events.js:314:20)
          at ClientRequest.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/node-request-interceptor/src/interceptors/ClientRequest/ClientRequestOverride.ts:292:14)
          at ClientRequest.emit (events.js:314:20)
          at Socket.socketErrorListener (_http_client.js:428:9)
          at Socket.emit (events.js:314:20)
          at emitErrorNT (internal/streams/destroy.js:92:8) undefined


console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Error: connect ECONNREFUSED 127.0.0.1:80
          at Object.dispatchError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
          at Request.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)
          at Request.emit (events.js:326:22)
          at Request.onRequestError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/request/request.js:877:8)
          at ClientRequestOverride.emit (events.js:314:20)
          at ClientRequest.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/node-request-interceptor/src/interceptors/ClientRequest/ClientRequestOverride.ts:292:14)
          at ClientRequest.emit (events.js:314:20)
          at Socket.socketErrorListener (_http_client.js:428:9)
          at Socket.emit (events.js:314:20)
          at emitErrorNT (internal/streams/destroy.js:92:8) undefined


console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Error: connect ECONNREFUSED 127.0.0.1:80
          at Object.dispatchError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
          at Request.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)
          at Request.emit (events.js:326:22)
          at Request.onRequestError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/request/request.js:877:8)
          at ClientRequestOverride.emit (events.js:314:20)
          at ClientRequest.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/node-request-interceptor/src/interceptors/ClientRequest/ClientRequestOverride.ts:292:14)
          at ClientRequest.emit (events.js:314:20)
          at Socket.socketErrorListener (_http_client.js:428:9)
          at Socket.emit (events.js:314:20)
          at emitErrorNT (internal/streams/destroy.js:92:8) undefined

从错误日志中看不到相关的文件路径,没法定位问题具体出在某个文件
解决办法:
1.出现此类报错大多数因为你在某个文件中需要mock接口请求数据,但是你没有做,导致jest就真正的发出了请求,又因为是测试环境,所以跟本没有反应,所以会报这个错误
2.需要你仔细审核自己的代码,看看到底是哪个接口没有mock数据,找到并mock(这才是最难的………………)就好了
3.注意点,由于我使用的是react-test-library的render方法,会渲染该组件的子组件,所以当你找不到时,需要考虑是不是有其他页面使用了该组件,也要审核一下

如有帮助,点个赞吧!