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

215 lines
6.3 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';
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';
import VueResource from 'vue-resource';
2018-10-15 14:42:47 +05:30
import Translate from '~/vue_shared/translate';
2018-11-18 11:00:15 +05:30
import jasmineDiff from 'jasmine-diff';
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
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) => {
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);
};
2017-09-10 17:25:29 +05:30
Vue.use(VueResource);
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;
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());
const builtinVueHttpInterceptors = Vue.http.interceptors.slice();
beforeEach(() => {
// restore interceptors so we have no remaining ones from previous tests
Vue.http.interceptors = builtinVueHttpInterceptors.slice();
});
2018-11-18 11:00:15 +05:30
let longRunningTestTimeoutHandle;
beforeEach(done => {
longRunningTestTimeoutHandle = setTimeout(() => {
done.fail('Test is running too long!');
}, 2000);
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
const testsContext = require.context('.', true, /_spec$/);
2018-05-09 12:01:36 +05:30
testsContext.keys().forEach(function(path) {
2017-08-17 22:00:37 +05:30
try {
testsContext(path);
} catch (err) {
2018-11-08 19:23:39 +05:30
console.log(err);
console.error('[GL SPEC RUNNER ERROR] Unable to load spec: ', path);
2018-05-09 12:01:36 +05:30
describe('Test bundle', function() {
it(`includes '${path}'`, function() {
2017-08-17 22:00:37 +05:30
expect(err).toBeNull();
});
});
}
});
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
});
2017-08-17 22:00:37 +05:30
// if we're generating coverage reports, make sure to include all files so
// that we can catch files with 0% coverage
// see: https://github.com/deepsweet/istanbul-instrumenter-loader/issues/15
if (process.env.BABEL_ENV === 'coverage') {
// exempt these files from the coverage report
const troubleMakers = [
'./blob_edit/blob_bundle.js',
2018-11-08 19:23:39 +05:30
'./boards/components/modal/empty_state.vue',
2018-03-27 19:54:05 +05:30
'./boards/components/modal/footer.js',
'./boards/components/modal/header.js',
2017-08-17 22:00:37 +05:30
'./cycle_analytics/cycle_analytics_bundle.js',
'./cycle_analytics/components/stage_plan_component.js',
'./cycle_analytics/components/stage_staging_component.js',
'./cycle_analytics/components/stage_test_component.js',
'./commit/pipelines/pipelines_bundle.js',
'./diff_notes/diff_notes_bundle.js',
'./diff_notes/components/jump_to_discussion.js',
'./diff_notes/components/resolve_count.js',
'./dispatcher.js',
'./environments/environments_bundle.js',
'./graphs/graphs_bundle.js',
'./issuable/time_tracking/time_tracking_bundle.js',
'./main.js',
'./merge_conflicts/merge_conflicts_bundle.js',
'./merge_conflicts/components/inline_conflict_lines.js',
'./merge_conflicts/components/parallel_conflict_lines.js',
'./monitoring/monitoring_bundle.js',
'./network/network_bundle.js',
'./network/branch_graph.js',
'./profile/profile_bundle.js',
'./protected_branches/protected_branches_bundle.js',
'./snippet/snippet_bundle.js',
'./terminal/terminal_bundle.js',
'./users/users_bundle.js',
'./issue_show/index.js',
];
2018-05-09 12:01:36 +05:30
describe('Uncovered files', function() {
2018-11-18 11:00:15 +05:30
const sourceFiles = require.context('~', true, /\.(js|vue)$/);
2018-03-27 19:54:05 +05:30
$.holdReady(true);
2018-05-09 12:01:36 +05:30
sourceFiles.keys().forEach(function(path) {
2017-08-17 22:00:37 +05:30
// ignore if there is a matching spec file
2018-11-18 11:00:15 +05:30
if (testsContext.keys().indexOf(`${path.replace(/\.(js|vue)$/, '')}_spec`) > -1) {
2017-08-17 22:00:37 +05:30
return;
}
2018-05-09 12:01:36 +05:30
it(`includes '${path}'`, function() {
2017-08-17 22:00:37 +05:30
try {
sourceFiles(path);
} catch (err) {
if (troubleMakers.indexOf(path) === -1) {
expect(err).toBeNull();
}
}
});
});
});
}