Commit be6f20d5 authored by Martin Hanzel's avatar Martin Hanzel

Fix test false passes on unmocked requests

parent 663b7bb4
......@@ -6,9 +6,10 @@ axios.isMock = true;
axios.defaults.adapter = config => {
const message =
`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);
error.config = config;
global.fail(error);
throw error;
};
......
/* eslint-disable global-require, promise/catch-or-return */
/* eslint-disable global-require */
import path from 'path';
......@@ -126,9 +126,8 @@ describe('mocks_helper.js', () => {
it('survives jest.isolateModules()', done => {
jest.isolateModules(() => {
const axios2 = require('~/lib/utils/axios_utils').default;
expect(axios2.get('http://gitlab.com'))
.rejects.toThrow('Unexpected unmocked request')
.then(done);
expect(axios2.isMock).toBe(true);
done();
});
});
......
......@@ -4,9 +4,11 @@ const $ = jest.requireActual('jquery');
// Fail tests for unmocked requests
$.ajax = () => {
throw new Error(
const err = new Error(
'Unexpected unmocked jQuery.ajax() call! Make sure to mock jQuery.ajax() in tests.',
);
global.fail(err);
throw err;
};
// jquery is not an ES6 module
......
......@@ -3,11 +3,22 @@ import axios from '~/lib/utils/axios_utils';
describe('Mock auto-injection', () => {
describe('mocks', () => {
it('~/lib/utils/axios_utils', () =>
expect(axios.get('http://gitlab.com')).rejects.toThrow('Unexpected unmocked request'));
let failMock;
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()', () => {
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