From be6f20d54f1dd2de65ce477c644fd5819dedf9e9 Mon Sep 17 00:00:00 2001 From: Martin Hanzel Date: Thu, 15 Aug 2019 14:04:43 -0400 Subject: Fix test false passes on unmocked requests --- spec/frontend/mocks/ce/lib/utils/axios_utils.js | 3 ++- spec/frontend/mocks/mocks_helper_spec.js | 7 +++---- spec/frontend/mocks/node/jquery.js | 4 +++- 3 files changed, 8 insertions(+), 6 deletions(-) (limited to 'spec/frontend/mocks') diff --git a/spec/frontend/mocks/ce/lib/utils/axios_utils.js b/spec/frontend/mocks/ce/lib/utils/axios_utils.js index b4065626b09..a3783b91f95 100644 --- a/spec/frontend/mocks/ce/lib/utils/axios_utils.js +++ b/spec/frontend/mocks/ce/lib/utils/axios_utils.js @@ -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; }; diff --git a/spec/frontend/mocks/mocks_helper_spec.js b/spec/frontend/mocks/mocks_helper_spec.js index b8bb02c2f43..82e88b712c0 100644 --- a/spec/frontend/mocks/mocks_helper_spec.js +++ b/spec/frontend/mocks/mocks_helper_spec.js @@ -1,4 +1,4 @@ -/* 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(); }); }); diff --git a/spec/frontend/mocks/node/jquery.js b/spec/frontend/mocks/node/jquery.js index 34a25772f67..5c82f65406e 100644 --- a/spec/frontend/mocks/node/jquery.js +++ b/spec/frontend/mocks/node/jquery.js @@ -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 -- cgit v1.2.1