Ошибка сети с аксиом на тест - PullRequest
0 голосов
/ 04 мая 2018

Я создаю приложение реакции с CRA, написал тестовый файл example.test.js. Этот файл содержит:

import axios from 'axios';


axios.get('http://localhost:3000/coins')
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error.response);
});

test('adds 1 + 2 to equal 3', () => {
  expect(3).toBe(3);
});  

Запустите тест, у меня есть:

 PASS  src/Api/Coins.test.js
  ✓ adds 1 + 2 to equal 3 (3ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.945s, estimated 1s
Ran all test suites related to changed files.

  console.log src/Api/Coins.test.js:9
    { Error: Network Error        at createError (/home/developer/Desktop/react-reason/cockpit/node_modules/axios/lib/core/createError.js:16:15)
        at XMLHttpRequest.handleError [as onerror] (/home/developer/Desktop/react-reason/cockpit/node_modules/axios/lib/adapters/xhr.js:87:14)
        at XMLHttpRequest.callback.(anonymous function) (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:289:32)
        at invokeEventListeners (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:219:27)
        at invokeInlineListeners (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:166:7)
        at EventTargetImpl._dispatch (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:122:7)
        at EventTargetImpl.dispatchEvent (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
        at XMLHttpRequest.dispatchEvent (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:61:35)
        at XMLHttpRequest.abort (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:405:16)
        at Object.abort (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:315:13)
        at RequestManager.close (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:146:21)
        at Window.close (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jsdom/lib/jsdom/browser/Window.js:362:29)
        at JSDOMEnvironment.dispose (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jest-environment-jsdom/build/index.js:44:19)
        at Promise.resolve.then (/home/developer/Desktop/react-reason/cockpit/node_modules/react-scripts/node_modules/jest/node_modules/jest-cli/build/runTest.js:102:17)
      config:
       { adapter: [Function: xhrAdapter],
         transformRequest: { '0': [Function: transformRequest] },
         transformResponse: { '0': [Function: transformResponse] },
         timeout: 0,
         xsrfCookieName: 'XSRF-TOKEN',
         xsrfHeaderName: 'X-XSRF-TOKEN',
         maxContentLength: -1,
         validateStatus: [Function: validateStatus],
         headers: { Accept: 'application/json, text/plain, */*' },
         method: 'get',
         url: 'http://localhost:3000/coins',
         data: undefined },
      request:
       XMLHttpRequest {
         onabort: null,
         onerror: { [Function: handleError] [Symbol(inline event listener wrapper)]: [Function] },
         onload: null,
         onloadend: null,
         onloadstart: null,
         onprogress: null,
         ontimeout: [Function: handleTimeout],
         upload:
          XMLHttpRequestUpload {
            onabort: null,
            onerror: null,
            onload: null,
            onloadend: null,
            onloadstart: null,
            onprogress: null,
            ontimeout: null,
            _ownerDocument: [Document],
            [Symbol(impl)]: [EventTargetImpl] },
         onreadystatechange: { [Function: handleLoad] [Symbol(inline event listener wrapper)]: [Function] },
         [Symbol(impl)]:
          EventTargetImpl {
            _eventListeners: {},
            [Symbol(wrapper)]: [Circular],
            [Symbol(DOM SymbolTree)]: [SymbolTreeNode] },
         [Symbol(flag)]:
          { synchronous: false,
            withCredentials: false,
            mimeType: null,
            auth: null,
            method: 'GET',
            responseType: '',
            requestHeaders: [Object],
            referrer: 'http://localhost/',
            uri: 'http://localhost:3000/coins',
            timeout: 0,
            body: undefined,
            formData: false,
            preflight: false,
            requestManager: [RequestManager],
            pool: [Object],
            agentOptions: [Object],
            strictSSL: true,
            proxy: undefined,
            cookieJar: [CookieJar],
            encoding: 'UTF-8',
            origin: 'http://localhost',
            userAgent: 'Node.js (linux; U; rv:v10.0.0) AppleWebKit/537.36 (KHTML, like Gecko)' },
         [Symbol(properties)]:
          { beforeSend: false,
            send: false,
            timeoutStart: 0,
            timeoutId: 0,
            timeoutFn: null,
            client: null,
            responseHeaders: {},
            filteredResponseHeaders: [],
            responseBuffer: null,
            responseCache: null,
            responseTextCache: null,
            responseXMLCache: null,
            responseURL: '',
            readyState: 0,
            status: 0,
            statusText: '',
            error: '',
            uploadComplete: true,
            abortError: true,
            cookieJar: [CookieJar],
            requestBuffer: null,
            requestCache: null,
            origin: 'http://localhost' } },
      response: undefined }

    Done in 149.43s.

Чтобы убедиться, что API работает, я сделал запрос с curl:

~/Desktop/react-reason/cockpit$ curl http://localhost:3000/coins
[
  {
    "coinSymbol": "BTC",
    "amount": 8.8
  },
  {
    "coinSymbol": "ETH",
    "amount": 12.4
  },
  {
    "coinSymbol": "XRP",
    "amount": 3412.48
  },
  {
    "coinSymbol": "ETH",
    "amount": 0.004
  }

Почему он не приносит никаких данных при запуске теста?

...