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

168 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-11-18 11:00:15 +05:30
/* eslint-disable
jasmine/no-global-setup, jasmine/no-unsafe-spy, no-underscore-dangle, no-console
*/
2018-10-15 14:42:47 +05:30
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';
2018-11-18 11:00:15 +05:30
import jasmineDiff from 'jasmine-diff';
2019-09-30 21:07:59 +05:30
import { config as testUtilsConfig } from '@vue/test-utils';
2020-01-01 13:55:28 +05:30
import Translate from '~/vue_shared/translate';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
import { getDefaultAdapter } from '~/lib/utils/axios_utils';
2018-05-09 12:01:36 +05:30
import { FIXTURES_PATH, TEST_HOST } from './test_constants';
import customMatchers from './matchers';
2018-03-17 18:26:18 +05:30
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')
.some(line => line.startsWith(' at VueWrapper.setProps ('));
if (isInVueTestUtils) {
return;
}
2018-03-17 18:26:18 +05:30
hasVueWarnings = true;
fail(`${msg}${trace}`);
};
let hasVueErrors = false;
2018-05-09 12:01:36 +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
}),
);
jasmine.addMatchers(customMatchers);
});
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;
2018-05-09 12:01:36 +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-10-15 14:42:47 +05:30
// Add global function to spy on a module's dependencies via rewire
window.spyOnDependency = (module, name) => {
const dependency = module.__GetDependency__(name);
const spy = jasmine.createSpy(name, dependency);
module.__Rewire__(name, spy);
return spy;
};
// Reset any rewired modules after each test (see babel-plugin-rewire)
afterEach(__rewire_reset_all__); // eslint-disable-line
2017-09-10 17:25:29 +05:30
// HACK: Chrome 59 disconnects if there are too many synchronous tests in a row
// because it appears to lock up the thread that communicates to Karma's socket
// This async beforeEach gets called on every spec and releases the JS thread long
// enough for the socket to continue to communicate.
// The downside is that it creates a minor performance penalty in the time it takes
// to run our unit tests.
beforeEach(done => done());
2018-11-18 11:00:15 +05:30
let longRunningTestTimeoutHandle;
beforeEach(done => {
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$/));
}
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-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', () => {
2018-05-09 12:01:36 +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
});