diff options
author | Kushal Pandya <kushalspandya@gmail.com> | 2019-09-03 15:26:49 +0300 |
---|---|---|
committer | Kushal Pandya <kushalspandya@gmail.com> | 2019-09-03 15:26:49 +0300 |
commit | b07bfbcdf616864be7d41ef3f582082f860286ae (patch) | |
tree | 19fb30ae7aaa16fca6447c10f23e674d50161798 /spec/frontend | |
parent | da0a5c7cc20194b71c41bd5572717fd9ac382cf4 (diff) | |
parent | be6f20d54f1dd2de65ce477c644fd5819dedf9e9 (diff) |
Merge branch 'mh/jest-fail-unmocked-requests' into 'master'
Make unmocked requests fail in Jest again
See merge request gitlab-org/gitlab-ce!31863
Diffstat (limited to 'spec/frontend')
-rw-r--r-- | spec/frontend/mocks/ce/lib/utils/axios_utils.js | 3 | ||||
-rw-r--r-- | spec/frontend/mocks/mocks_helper_spec.js | 7 | ||||
-rw-r--r-- | spec/frontend/mocks/node/jquery.js | 4 | ||||
-rw-r--r-- | spec/frontend/mocks_spec.js | 15 |
4 files changed, 21 insertions, 8 deletions
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 diff --git a/spec/frontend/mocks_spec.js b/spec/frontend/mocks_spec.js index 2d2324120fd..a4a1fdea396 100644 --- a/spec/frontend/mocks_spec.js +++ b/spec/frontend/mocks_spec.js @@ -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); }); }); }); |