debian-mirror-gitlab/spec/javascripts/test_bundle.js

146 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-11-18 11:00:15 +05:30
/* eslint-disable
2020-10-24 23:57:45 +05:30
jasmine/no-global-setup, no-underscore-dangle, no-console
2018-11-18 11:00:15 +05:30
*/
2018-10-15 14:42:47 +05:30
2021-03-11 19:13:27 +05:30
import { config as testUtilsConfig } from '@vue/test-utils';
import jasmineDiff from 'jasmine-diff';
2017-09-10 17:25:29 +05:30
import $ from 'jquery';
2019-09-30 21:07:59 +05:30
import 'core-js/features/set-immediate';
2018-03-27 19:54:05 +05:30
import 'vendor/jasmine-jquery';
2017-09-10 17:25:29 +05:30
import '~/commons';
import Vue from 'vue';
2021-03-11 19:13:27 +05:30
import { getDefaultAdapter } from '~/lib/utils/axios_utils';
2020-01-01 13:55:28 +05:30
import Translate from '~/vue_shared/translate';
2017-09-10 17:25:29 +05:30
2018-05-09 12:01:36 +05:30
import { FIXTURES_PATH, TEST_HOST } from './test_constants';
2019-09-30 21:07:59 +05:30
// Tech debt issue TBD
testUtilsConfig.logModifiedComponents = false;
2017-09-10 17:25:29 +05:30
const isHeadlessChrome = /\bHeadlessChrome\//.test(navigator.userAgent);
Vue.config.devtools = !isHeadlessChrome;
Vue.config.productionTip = false;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
let hasVueWarnings = false;
Vue.config.warnHandler = (msg, vm, trace) => {
2019-03-02 22:35:43 +05:30
// The following workaround is necessary, so we are able to use setProps from Vue test utils
// see https://github.com/vuejs/vue-test-utils/issues/631#issuecomment-421108344
const currentStack = new Error().stack;
const isInVueTestUtils = currentStack
.split('\n')
2021-03-08 18:12:59 +05:30
.some((line) => line.startsWith(' at VueWrapper.setProps ('));
2019-03-02 22:35:43 +05:30
if (isInVueTestUtils) {
return;
}
2018-03-17 18:26:18 +05:30
hasVueWarnings = true;
fail(`${msg}${trace}`);
};
let hasVueErrors = false;
2021-03-08 18:12:59 +05:30
Vue.config.errorHandler = function (err) {
2018-03-17 18:26:18 +05:30
hasVueErrors = true;
fail(err);
};
2018-10-15 14:42:47 +05:30
Vue.use(Translate);
2017-09-10 17:25:29 +05:30
// enable test fixtures
2018-05-09 12:01:36 +05:30
jasmine.getFixtures().fixturesPath = FIXTURES_PATH;
jasmine.getJSONFixtures().fixturesPath = FIXTURES_PATH;
2018-11-18 11:00:15 +05:30
beforeAll(() => {
jasmine.addMatchers(
jasmineDiff(jasmine, {
2018-12-05 23:21:45 +05:30
colors: window.__karma__.config.color,
inline: window.__karma__.config.color,
2018-11-18 11:00:15 +05:30
}),
);
});
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
// globalize common libraries
2018-11-08 19:23:39 +05:30
window.$ = $;
window.jQuery = window.$;
2017-08-17 22:00:37 +05:30
// stub expected globals
window.gl = window.gl || {};
2018-05-09 12:01:36 +05:30
window.gl.TEST_HOST = TEST_HOST;
2017-08-17 22:00:37 +05:30
window.gon = window.gon || {};
2018-03-27 19:54:05 +05:30
window.gon.test_env = true;
2019-12-21 20:55:43 +05:30
window.gon.ee = process.env.IS_EE;
2018-05-09 12:01:36 +05:30
gon.relative_url_root = '';
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
let hasUnhandledPromiseRejections = false;
2021-03-08 18:12:59 +05:30
window.addEventListener('unhandledrejection', (event) => {
2017-09-10 17:25:29 +05:30
hasUnhandledPromiseRejections = true;
console.error('Unhandled promise rejection:');
console.error(event.reason.stack || event.reason);
});
2018-11-18 11:00:15 +05:30
let longRunningTestTimeoutHandle;
2021-03-08 18:12:59 +05:30
beforeEach((done) => {
2018-11-18 11:00:15 +05:30
longRunningTestTimeoutHandle = setTimeout(() => {
done.fail('Test is running too long!');
2019-09-04 21:01:54 +05:30
}, 4000);
2018-11-18 11:00:15 +05:30
done();
});
afterEach(() => {
clearTimeout(longRunningTestTimeoutHandle);
});
2018-03-17 18:26:18 +05:30
const axiosDefaultAdapter = getDefaultAdapter();
2017-08-17 22:00:37 +05:30
// render all of our tests
2019-07-07 11:18:12 +05:30
const testContexts = [require.context('spec', true, /_spec$/)];
2019-12-21 20:55:43 +05:30
if (process.env.IS_EE) {
2019-07-07 11:18:12 +05:30
testContexts.push(require.context('ee_spec', true, /_spec$/));
}
2021-03-08 18:12:59 +05:30
testContexts.forEach((context) => {
context.keys().forEach((path) => {
2019-07-07 11:18:12 +05:30
try {
context(path);
} catch (err) {
console.log(err);
console.error('[GL SPEC RUNNER ERROR] Unable to load spec: ', path);
2021-03-08 18:12:59 +05:30
describe('Test bundle', function () {
it(`includes '${path}'`, function () {
2019-07-07 11:18:12 +05:30
expect(err).toBeNull();
});
2017-08-17 22:00:37 +05:30
});
2019-07-07 11:18:12 +05:30
}
});
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
describe('test errors', () => {
2021-03-08 18:12:59 +05:30
beforeAll((done) => {
2018-03-17 18:26:18 +05:30
if (hasUnhandledPromiseRejections || hasVueWarnings || hasVueErrors) {
setTimeout(done, 1000);
} else {
done();
}
});
it('has no unhandled Promise rejections', () => {
expect(hasUnhandledPromiseRejections).toBe(false);
});
it('has no Vue warnings', () => {
expect(hasVueWarnings).toBe(false);
});
it('has no Vue error', () => {
expect(hasVueErrors).toBe(false);
});
it('restores axios adapter after mocking', () => {
if (getDefaultAdapter() !== axiosDefaultAdapter) {
fail('axios adapter is not restored! Did you forget a restore() on MockAdapter?');
}
});
2017-09-10 17:25:29 +05:30
});