ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.42k stars 241 forks source link

This package actually does the requests to the API instead of mocking responses #351

Closed sinner closed 1 year ago

sinner commented 1 year ago

I'm using the following version of the packages:

"axios": "^1.1.3"
"axios-mock-adapter": "^1.21.2"
"vue": "^2.6.14"

I followed the setting up instructions and this package actually does the requests to the API instead of mocking responses. It is causing several Network Errors in our deploying process because of that behavior.

This is a sample error when there is no internet conection:

console.log src/util/CollectionsUtil.js:87
      Error: Network Error
          at Error.apply (D:\jenkins\workspace\pipeline\ui-app\node_modules\core-js\internals\wrap-error-constructor-with-cause.js:37:62)
          at new Error (D:\jenkins\workspace\pipeline\ui-app\node_modules\core-js\modules\es.error.cause.js:28:43)
          at Object.createAxiosError (D:\jenkins\workspace\pipeline\ui-app\node_modules\axios-mock-adapter\src\utils.js:143:15)
          at Array.<anonymous> (D:\jenkins\workspace\pipeline\ui-app\node_modules\axios-mock-adapter\src\index.js:146:29)
          at handleRequest (D:\jenkins\workspace\pipeline\ui-app\node_modules\axios-mock-adapter\src\handle_request.js:75:30)
          at D:\jenkins\workspace\pipeline\ui-app\node_modules\axios-mock-adapter\src\index.js:23:7
          at new Promise (<anonymous>)
          at MockAdapter.adapter (D:\jenkins\workspace\pipeline\ui-app\node_modules\axios-mock-adapter\src\index.js:22:12)
          at dispatchRequest (D:\jenkins\workspace\pipeline\ui-app\node_modules\axios\lib\core\dispatchRequest.js:52:10)
          at processTicksAndRejections (internal/process/task_queues.js:89:5)
          at validateCollectionItemNameUniqueness (D:\jenkins\workspace\pipeline\ui-app\awse3.0.5\src\util\CollectionsUtil.js:60:18)
          at Object.getDuplicateName (D:\jenkins\workspace\pipeline\ui-app\src\util\CollectionsUtil.js:221:14)
          at Object.<anonymous> (D:\jenkins\workspace\pipeline\ui-app\tests\unit\util\CollectionsUtil.spec.js:172:17) {
        isAxiosError: true,
        config: {
          url: 'http://localhost:61786/api/v1/data',
          method: 'post',
          data: '{ search: test }',
          headers: {
            Accept: 'application/json, text/plain, */*',
            'Content-Type': 'application/json;charset=utf-8'
          },
          transformRequest: [ [Function: transformRequest] ],
          transformResponse: [ [Function: transformResponse] ],
          timeout: 0,
          xsrfCookieName: 'XSRF-TOKEN',
          xsrfHeaderName: 'X-XSRF-TOKEN',
          maxContentLength: -1,
          validateStatus: [Function: validateStatus]
        },
        toJSON: [Function: toJSON]
      }
antoniel commented 1 year ago

Hey @sinner, may you provide the sample code you used to set up the mock? That way, we can find if anything is missing in your setup; if you could set up a codesandbox would be even easier to investigate o/

sinner commented 1 year ago

@antoniel Sure, this is pretty much the code. That component method loadAsyncData uses axios to call the API, but in my tests I don't want to actually call the API. I expect the tests run even if the jenkins serve doesn't have internet connection.

  import { shallowMount } from '@vue/test-utils';
  import axios from 'axios';
  import MockAdapter from 'axios-mock-adapter';
  import flushPromises from 'flush-promises';
  import MockSecMktRules from '../__mocks__/SecMarketingRules.json';

  const mock = new MockAdapter(axios);

  describe('Component test', () => {
    beforeEach(() => {
      mock.restore();
      mock
        .onGet()
        .reply(200, MockSecMktRules.success)
        .onAny()
        .reply(200, MockSecMktRules.success)
        .passThrough();
    });

    test('behavior', async done => {
      const wrapper = shallowMount(MockComponent);

      await wrapper.vm.loadAsyncData();
    });

  });
marcbachmann commented 1 year ago

I think this was caused by an axios v1 incompatibility. This should be fixed in newer versions.