diff options
author | Winnie Hellmann <winnie@gitlab.com> | 2018-04-23 19:18:30 +0200 |
---|---|---|
committer | Winnie Hellmann <winnie@gitlab.com> | 2018-07-23 13:44:06 +0200 |
commit | f6e28443a526f6b24c17d34ed302267897e04650 (patch) | |
tree | 67f624c5324846c797fb74b0fc75d46c6c5beb3e | |
parent | f6d13ef53aa50541ec0c751156424d7c49bf3322 (diff) | |
download | gitlab-ce-winh-mock-axios-requests.tar.gz |
Fail tests if Axios is not mockedwinh-mock-axios-requests
-rw-r--r-- | app/assets/javascripts/lib/utils/axios_utils.js | 37 | ||||
-rw-r--r-- | spec/javascripts/test_bundle.js | 9 |
2 files changed, 27 insertions, 19 deletions
diff --git a/app/assets/javascripts/lib/utils/axios_utils.js b/app/assets/javascripts/lib/utils/axios_utils.js index 792871e2ecf..651f9a3bc4e 100644 --- a/app/assets/javascripts/lib/utils/axios_utils.js +++ b/app/assets/javascripts/lib/utils/axios_utils.js @@ -7,7 +7,7 @@ axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; // Maintain a global counter for active requests // see: spec/support/wait_for_requests.rb -axios.interceptors.request.use((config) => { +axios.interceptors.request.use(config => { window.activeVueResources = window.activeVueResources || 0; window.activeVueResources += 1; @@ -15,22 +15,27 @@ axios.interceptors.request.use((config) => { }); // Remove the global counter -axios.interceptors.response.use((config) => { - window.activeVueResources -= 1; +axios.interceptors.response.use( + config => { + window.activeVueResources -= 1; - return config; -}, (e) => { - window.activeVueResources -= 1; + return config; + }, + e => { + window.activeVueResources -= 1; - return Promise.reject(e); -}); + return Promise.reject(e); + }, +); -export default axios; +const originalGet = axios.get; +axios.get = function(url, config) { + if (axios.defaults.adapter.isNotMocked) { + fail(`Request is not mocked: ${url}`); + return Promise.reject(); + } -/** - * @return The adapter that axios uses for dispatching requests. This may be overwritten in tests. - * - * @see https://github.com/axios/axios/tree/master/lib/adapters - * @see https://github.com/ctimmerm/axios-mock-adapter/blob/v1.12.0/src/index.js#L39 - */ -export const getDefaultAdapter = () => axios.defaults.adapter; + return originalGet.apply(this, [url, config]); +}; + +export default axios; diff --git a/spec/javascripts/test_bundle.js b/spec/javascripts/test_bundle.js index bc00fdfd73c..33b3ee430b2 100644 --- a/spec/javascripts/test_bundle.js +++ b/spec/javascripts/test_bundle.js @@ -8,7 +8,7 @@ import VueResource from 'vue-resource'; import Translate from '~/vue_shared/translate'; import jasmineDiff from 'jasmine-diff'; -import { getDefaultAdapter } from '~/lib/utils/axios_utils'; +import axios from '~/lib/utils/axios_utils'; import { FIXTURES_PATH, TEST_HOST } from './test_constants'; import customMatchers from './matchers'; @@ -91,7 +91,10 @@ beforeEach(() => { Vue.http.interceptors = builtinVueHttpInterceptors.slice(); }); -const axiosDefaultAdapter = getDefaultAdapter(); +// see https://github.com/axios/axios/tree/master/lib/adapters +const axiosDummyAdapter = () => fail('Axios request not mocked!'); +axiosDummyAdapter.isNotMocked = true; +axios.defaults.adapter = axiosDummyAdapter; // render all of our tests const testsContext = require.context('.', true, /_spec$/); @@ -131,7 +134,7 @@ describe('test errors', () => { }); it('restores axios adapter after mocking', () => { - if (getDefaultAdapter() !== axiosDefaultAdapter) { + if (axios.defaults.adapter !== axiosDummyAdapter) { fail('axios adapter is not restored! Did you forget a restore() on MockAdapter?'); } }); |