gitlab-org--gitlab-foss/spec/javascripts/test_bundle.js

146 lines
3.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable
jasmine/no-global-setup, no-underscore-dangle, no-console
*/
2018-03-30 17:58:34 +00:00
import { config as testUtilsConfig } from '@vue/test-utils';
import jasmineDiff from 'jasmine-diff';
import $ from 'jquery';
2019-07-11 19:32:37 +00:00
import 'core-js/features/set-immediate';
2018-02-20 22:20:48 +00:00
import 'vendor/jasmine-jquery';
import '~/commons';
2017-06-20 08:05:55 +00:00
import Vue from 'vue';
import { getDefaultAdapter } from '~/lib/utils/axios_utils';
import Translate from '~/vue_shared/translate';
2018-04-08 10:20:05 +00:00
import { FIXTURES_PATH, TEST_HOST } from './test_constants';
// Tech debt issue TBD
testUtilsConfig.logModifiedComponents = false;
2017-06-22 11:11:05 +00:00
const isHeadlessChrome = /\bHeadlessChrome\//.test(navigator.userAgent);
Vue.config.devtools = !isHeadlessChrome;
Vue.config.productionTip = false;
2017-11-02 14:17:57 +00:00
let hasVueWarnings = false;
Vue.config.warnHandler = (msg, vm, trace) => {
// 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')
.some((line) => line.startsWith(' at VueWrapper.setProps ('));
if (isInVueTestUtils) {
return;
}
2017-11-02 14:17:57 +00:00
hasVueWarnings = true;
fail(`${msg}${trace}`);
};
let hasVueErrors = false;
Vue.config.errorHandler = function (err) {
hasVueErrors = true;
fail(err);
};
Vue.use(Translate);
2017-06-20 08:05:55 +00:00
// enable test fixtures
2018-04-08 10:20:05 +00:00
jasmine.getFixtures().fixturesPath = FIXTURES_PATH;
jasmine.getJSONFixtures().fixturesPath = FIXTURES_PATH;
beforeAll(() => {
jasmine.addMatchers(
jasmineDiff(jasmine, {
2018-09-14 13:40:53 +00:00
colors: window.__karma__.config.color,
inline: window.__karma__.config.color,
}),
);
});
// globalize common libraries
window.$ = $;
window.jQuery = window.$;
// stub expected globals
window.gl = window.gl || {};
2018-04-08 10:20:05 +00:00
window.gl.TEST_HOST = TEST_HOST;
window.gon = window.gon || {};
2018-03-01 18:13:50 +00:00
window.gon.test_env = true;
window.gon.ee = process.env.IS_EE;
gon.relative_url_root = '';
let hasUnhandledPromiseRejections = false;
window.addEventListener('unhandledrejection', (event) => {
hasUnhandledPromiseRejections = true;
console.error('Unhandled promise rejection:');
console.error(event.reason.stack || event.reason);
});
let longRunningTestTimeoutHandle;
beforeEach((done) => {
longRunningTestTimeoutHandle = setTimeout(() => {
done.fail('Test is running too long!');
}, 4000);
done();
});
afterEach(() => {
clearTimeout(longRunningTestTimeoutHandle);
});
const axiosDefaultAdapter = getDefaultAdapter();
// render all of our tests
const testContexts = [require.context('spec', true, /_spec$/)];
if (process.env.IS_EE) {
testContexts.push(require.context('ee_spec', true, /_spec$/));
}
testContexts.forEach((context) => {
context.keys().forEach((path) => {
try {
context(path);
} catch (err) {
console.log(err);
console.error('[GL SPEC RUNNER ERROR] Unable to load spec: ', path);
describe('Test bundle', function () {
it(`includes '${path}'`, function () {
expect(err).toBeNull();
});
});
}
});
});
2017-11-02 14:17:57 +00:00
describe('test errors', () => {
beforeAll((done) => {
if (hasUnhandledPromiseRejections || hasVueWarnings || hasVueErrors) {
2017-11-02 14:17:57 +00:00
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?');
}
});
});