gitlab-org--gitlab-foss/spec/frontend/test_setup.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-02-20 15:26:44 -05:00
import Vue from 'vue';
2019-04-01 14:55:57 -04:00
import * as jqueryMatchers from 'custom-jquery-matchers';
2019-06-21 23:35:51 -04:00
import $ from 'jquery';
import { config as testUtilsConfig } from '@vue/test-utils';
import Translate from '~/vue_shared/translate';
import { initializeTestTimeout } from './helpers/timeout';
import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './helpers/fixtures';
import { setupManualMocks } from './mocks/mocks_helper';
import customMatchers from './matchers';
2019-02-20 14:59:44 -05:00
import './helpers/dom_shims';
2019-06-21 23:35:51 -04:00
// Expose jQuery so specs using jQuery plugins can be imported nicely.
// Here is an issue to explore better alternatives:
// https://gitlab.com/gitlab-org/gitlab/issues/12448
2019-06-21 23:35:51 -04:00
window.jQuery = $;
process.on('unhandledRejection', global.promiseRejectionHandler);
setupManualMocks();
2019-03-25 16:32:48 -04:00
afterEach(() =>
// give Promises a bit more time so they fail the right test
new Promise(setImmediate).then(() => {
// wait for pending setTimeout()s
jest.runAllTimers();
}),
);
2019-03-21 13:05:21 -04:00
initializeTestTimeout(process.env.CI ? 5000 : 500);
2019-02-20 14:59:44 -05:00
2019-03-28 09:17:52 -04:00
Vue.config.devtools = false;
Vue.config.productionTip = false;
2019-02-20 15:26:44 -05:00
Vue.use(Translate);
2019-03-28 15:42:32 -04:00
// convenience wrapper for migration from Karma
Object.assign(global, {
getJSONFixture,
loadFixtures: loadHTMLFixture,
setFixtures: setHTMLFixture,
2019-05-15 08:18:54 -04:00
// The following functions fill the fixtures cache in Karma.
// This is not necessary in Jest because we make no Ajax request.
loadJSONFixtures() {},
preloadFixtures() {},
});
2019-04-01 14:55:57 -04:00
Object.assign(global, {
MutationObserver() {
return {
disconnect() {},
observe() {},
};
},
});
2019-04-01 14:55:57 -04:00
// custom-jquery-matchers was written for an old Jest version, we need to make it compatible
Object.entries(jqueryMatchers).forEach(([matcherName, matcherFactory]) => {
// Don't override existing Jest matcher
if (matcherName === 'toHaveLength') {
return;
}
2019-04-01 14:55:57 -04:00
expect.extend({
[matcherName]: matcherFactory().compare,
});
});
expect.extend(customMatchers);
// Tech debt issue TBD
testUtilsConfig.logModifiedComponents = false;
// Basic stub for MutationObserver
global.MutationObserver = () => ({
disconnect: () => {},
observe: () => {},
});
Object.assign(global, {
requestIdleCallback(cb) {
const start = Date.now();
return setTimeout(() => {
cb({
didTimeout: false,
timeRemaining: () => Math.max(0, 50 - (Date.now() - start)),
});
});
},
cancelIdleCallback(id) {
clearTimeout(id);
},
});
// make sure that each test actually tests something
// see https://jestjs.io/docs/en/expect#expecthasassertions
beforeEach(() => {
expect.hasAssertions();
});