jest报错

来源:12-8 请求模块单元测试 jasmine-ajax + 测试代码编写

慕用3095910

2019-12-27

console.error node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/virtual-console.js:29
Error: Error: connect ECONNREFUSED 127.0.0.1:80
at Object.dispatchError (/Users/chenyiyang/ts-axios-new/node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/living/xhr-utils.js:65:19)
at Request.client.on.err (/Users/chenyiyang/ts-axios-new/node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/living/xmlhttprequest.js:676:20)
at Request.emit (events.js:187:15)
at Request.onRequestError (/Users/chenyiyang/ts-axios-new/node_modules/_request@2.88.0@request/request.js:881:8)
at ClientRequest.emit (events.js:182:13)
at Socket.socketErrorListener (_http_client.js:392:9)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19) undefined
console.error node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [Error: expect(received).toBe(expected) // Object.is equality

  Expected: "object"
  Received: "string"]
      at reportException (/Users/chenyiyang/ts-axios-new/node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
      at Timeout.callback [as _onTimeout] (/Users/chenyiyang/ts-axios-new/node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/browser/Window.js:680:7)
      at ontimeout (timers.js:436:11)
      at tryOnTimeout (timers.js:300:5)
      at listOnTimeout (timers.js:263:5)
      at Timer.processTimers (timers.js:223:10) { Error: expect(received).toBe(expected) // Object.is equality
  
  Expected: "object"
  Received: "string"
      at /Users/chenyiyang/ts-axios-new/test/requests.spec.ts:204:38
      at Timeout.callback [as _onTimeout] (/Users/chenyiyang/ts-axios-new/node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/browser/Window.js:678:19)
      at ontimeout (timers.js:436:11)
      at tryOnTimeout (timers.js:300:5)
      at listOnTimeout (timers.js:263:5)
      at Timer.processTimers (timers.js:223:10)
    matcherResult:
     { actual: 'string',
       expected: 'object',
       message: [Function],
       name: 'toBe',
       pass: false } }

● requests › should reject on network errors

expect(received).toBe(expected) // Object.is equality

Expected: "post"
Received: "POST"

  25 |       method: 'POST'
  26 |     }).then(response => {
> 27 |       expect(response.config.method).toBe('post')
     |                                      ^
  28 |     })
  29 | 
  30 |     return getAjaxRequest().then(request => {

  at test/requests.spec.ts:27:38

● requests › should reject on network errors

MockAjax not installed.

   8 | 
   9 |   afterEach(() => {
> 10 |     jasmine.Ajax.uninstall()
     |                  ^
  11 |   })
  12 | 
  13 |   test('should treat single string arg as url', () => {

  at MockAjax.uninstall (node_modules/_jasmine-ajax@4.0.0@jasmine-ajax/lib/mock-ajax.js:583:15)
  at Object.<anonymous> (test/requests.spec.ts:10:18)

● requests › should reject when request timeout

expect(received).toEqual(expected) // deep equality

Expected: Any<FakeXMLHttpRequest>
Received: {"_eventHandlers": {"readystatechange": [Function handleLoad]}, "_registeredHandlers": Set {"readystatechange"}, "upload": {"_ownerDocument": {"location": {"assign": [Function assign], "hash": "", "host": "localhost", "hostname": "localhost", "href": "http://localhost/", "origin": "http://localhost", "pathname": "/", "port": "", "protocol": "http:", "reload": [Function reload], "replace": [Function replace], "search": "", "toString": [Function toString]}}}, Symbol(flag): {"agentOptions": undefined, "auth": null, "body": undefined, "cookieJar": {"cookies": [], "rejectPublicSuffixes": true, "storeType": "MemoryCookieStore", "version": "tough-cookie@2.5.0"}, "encoding": "UTF-8", "formData": false, "method": "GET", "mimeType": null, "origin": "http://localhost", "pool": undefined, "preflight": false, "proxy": undefined, "referrer": "http://localhost/", "requestHeaders": {"Accept": "application/json, text/plain, */*"}, "requestManager": {"openedRequests": []}, "responseType": "", "strictSSL": undefined, "synchronous": false, "timeout": 0, "uri": "http://localhost/foo", "userAgent": "Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/11.12.0", "withCredentials": false}, Symbol(properties): {"abortError": false, "beforeSend": false, "bufferStepSize": 1048576, "client": {"headers": {"Accept": "application/json, text/plain, */*", "Accept-Language": "en", "Referer": "http://localhost/", "User-Agent": "Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/11.12.0", "accept-encoding": "gzip, deflate", "host": "localhost"}, "method": "GET", "uri": {"auth": null, "hash": null, "host": "localhost", "hostname": "localhost", "href": "http://localhost/foo", "path": "/foo", "pathname": "/foo", "port": 80, "protocol": "http:", "query": null, "search": null, "slashes": true}}, "cookieJar": {"cookies": [], "rejectPublicSuffixes": true, "storeType": "MemoryCookieStore", "version": "tough-cookie@2.5.0"}, "error": [Error: connect ECONNREFUSED 127.0.0.1:80], "filteredResponseHeaders": [], "origin": "http://localhost", "readyState": 4, "requestBuffer": null, "requestCache": null, "responseBuffer": null, "responseCache": null, "responseHeaders": {}, "responseTextCache": null, "responseURL": "", "responseXMLCache": null, "send": false, "status": 0, "statusText": "", "timeoutFn": null, "timeoutId": 0, "timeoutStart": 0, "totalReceivedChunkSize": 0, "uploadComplete": true, "uploadListener": false}}

  56 |       expect(reason instanceof Error).toBeTruthy()
  57 |       expect((reason as AxiosError).message).toBe('Network Error')
> 58 |       expect(reason.request).toEqual(expect.any(XMLHttpRequest))
     |                              ^
  59 | 
  60 |       jasmine.Ajax.install()
  61 |     }

  at next (test/requests.spec.ts:58:30)

● requests › should return JSON when rejecting

expect(received).toBe(expected) // Object.is equality

Expected: "object"
Received: "string"

  202 | 
  203 |       setTimeout(() => {
> 204 |         expect(typeof response.data).toBe('object')
      |                                      ^
  205 |         expect(response.data.error).toBe('BAD USERNAME')
  206 |         expect(response.data.code).toBe(1)
  207 |         done()

  at test/requests.spec.ts:204:38
  at Timeout.callback [as _onTimeout] (node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/browser/Window.js:678:19)

老师,我jest是最新的,但是不能install和uninstall
"jest": “^24.9.0”,
“jest-config”: “^24.9.0”,
那这个问题该咋办

写回答

1回答

ustbhuangyi

2019-12-27

你这个看上去就是测试没通过啊,没其他问题吧

0
2
Aioros2017
回复
慕用3095910
我也遇到了这个报错: console.error node_modules/_jsdom@11.12.0@jsdom/lib/jsdom/virtual-console.js:29 Error: Error: connect ECONNREFUSED 127.0.0.1:80 请问你是怎么解决的?
2020-07-16
共2条回复

下一代前端开发语言 TypeScript从零重构axios

课程从零开始重构功能完整的JS库,是学习造轮子的不二之选!

2631 学习 · 877 问题

查看课程