Commit cc888e99 authored by Martin Hanzel's avatar Martin Hanzel Committed by Kushal Pandya

Fix test false passes on unmocked requests

Update tests to always mock async method calls
with return values or stub them.
parent 66be6eb1
...@@ -6,9 +6,10 @@ axios.isMock = true; ...@@ -6,9 +6,10 @@ axios.isMock = true;
axios.defaults.adapter = config => { axios.defaults.adapter = config => {
const message = const message =
`Unexpected unmocked request: ${JSON.stringify(config, null, 2)}\n` + `Unexpected unmocked request: ${JSON.stringify(config, null, 2)}\n` +
'Consider using the `axios-mock-adapter` in tests.'; 'Consider using the `axios-mock-adapter` module in tests.';
const error = new Error(message); const error = new Error(message);
error.config = config; error.config = config;
global.fail(error);
throw error; throw error;
}; };
......
/* eslint-disable global-require, promise/catch-or-return */ /* eslint-disable global-require */
import path from 'path'; import path from 'path';
...@@ -126,9 +126,8 @@ describe('mocks_helper.js', () => { ...@@ -126,9 +126,8 @@ describe('mocks_helper.js', () => {
it('survives jest.isolateModules()', done => { it('survives jest.isolateModules()', done => {
jest.isolateModules(() => { jest.isolateModules(() => {
const axios2 = require('~/lib/utils/axios_utils').default; const axios2 = require('~/lib/utils/axios_utils').default;
expect(axios2.get('http://gitlab.com')) expect(axios2.isMock).toBe(true);
.rejects.toThrow('Unexpected unmocked request') done();
.then(done);
}); });
}); });
......
...@@ -4,9 +4,11 @@ const $ = jest.requireActual('jquery'); ...@@ -4,9 +4,11 @@ const $ = jest.requireActual('jquery');
// Fail tests for unmocked requests // Fail tests for unmocked requests
$.ajax = () => { $.ajax = () => {
throw new Error( const err = new Error(
'Unexpected unmocked jQuery.ajax() call! Make sure to mock jQuery.ajax() in tests.', 'Unexpected unmocked jQuery.ajax() call! Make sure to mock jQuery.ajax() in tests.',
); );
global.fail(err);
throw err;
}; };
// jquery is not an ES6 module // jquery is not an ES6 module
......
...@@ -3,11 +3,22 @@ import axios from '~/lib/utils/axios_utils'; ...@@ -3,11 +3,22 @@ import axios from '~/lib/utils/axios_utils';
describe('Mock auto-injection', () => { describe('Mock auto-injection', () => {
describe('mocks', () => { describe('mocks', () => {
it('~/lib/utils/axios_utils', () => let failMock;
expect(axios.get('http://gitlab.com')).rejects.toThrow('Unexpected unmocked request')); beforeEach(() => {
failMock = jest.spyOn(global, 'fail').mockImplementation();
});
it('~/lib/utils/axios_utils', done => {
expect(axios.get('http://gitlab.com')).rejects.toThrow('Unexpected unmocked request');
setImmediate(() => {
expect(failMock).toHaveBeenCalledTimes(1);
done();
});
});
it('jQuery.ajax()', () => { it('jQuery.ajax()', () => {
expect($.ajax).toThrow('Unexpected unmocked'); expect($.ajax).toThrow('Unexpected unmocked');
expect(failMock).toHaveBeenCalledTimes(1);
}); });
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment