debian-mirror-gitlab/spec/frontend/test_setup.js

101 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { config as testUtilsConfig } from '@vue/test-utils';
import * as jqueryMatchers from 'custom-jquery-matchers';
2019-07-07 11:18:12 +05:30
import Vue from 'vue';
2021-01-03 14:25:43 +05:30
import 'jquery';
2021-03-11 19:13:27 +05:30
import { setGlobalDateToFakeDate } from 'helpers/fake_date';
2020-01-01 13:55:28 +05:30
import Translate from '~/vue_shared/translate';
2021-03-08 18:12:59 +05:30
import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './__helpers__/fixtures';
2021-03-11 19:13:27 +05:30
import { initializeTestTimeout } from './__helpers__/timeout';
2019-10-12 21:52:04 +05:30
import customMatchers from './matchers';
2021-03-11 19:13:27 +05:30
import { setupManualMocks } from './mocks/mocks_helper';
2019-09-30 21:07:59 +05:30
2021-03-08 18:12:59 +05:30
import './__helpers__/dom_shims';
import './__helpers__/jquery';
2020-04-22 19:07:51 +05:30
import '~/commons/bootstrap';
2019-02-15 15:39:39 +05:30
2021-03-08 18:12:59 +05:30
// This module has some fairly decent visual test coverage in it's own repository.
jest.mock('@gitlab/favicon-overlay');
2019-07-07 11:18:12 +05:30
process.on('unhandledRejection', global.promiseRejectionHandler);
2019-02-15 15:39:39 +05:30
2019-09-30 21:07:59 +05:30
setupManualMocks();
2021-03-11 19:13:27 +05:30
// Fake the `Date` for the rest of the jest spec runtime environment.
// https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39496#note_503084332
setGlobalDateToFakeDate();
2019-07-07 11:18:12 +05:30
afterEach(() =>
// give Promises a bit more time so they fail the right test
new Promise(setImmediate).then(() => {
// wait for pending setTimeout()s
2020-04-22 19:07:51 +05:30
jest.runOnlyPendingTimers();
2019-07-07 11:18:12 +05:30
}),
);
2021-03-08 18:12:59 +05:30
initializeTestTimeout(process.env.CI ? 6000 : 500);
2019-07-07 11:18:12 +05:30
Vue.config.devtools = false;
Vue.config.productionTip = false;
Vue.use(Translate);
// convenience wrapper for migration from Karma
Object.assign(global, {
2019-12-04 20:38:33 +05:30
getJSONFixture,
2019-07-07 11:18:12 +05:30
loadFixtures: loadHTMLFixture,
setFixtures: setHTMLFixture,
2019-09-04 21:01:54 +05:30
// The following functions fill the fixtures cache in Karma.
// This is not necessary in Jest because we make no Ajax request.
loadJSONFixtures() {},
preloadFixtures() {},
2019-02-15 15:39:39 +05:30
});
2019-07-07 11:18:12 +05:30
// custom-jquery-matchers was written for an old Jest version, we need to make it compatible
Object.entries(jqueryMatchers).forEach(([matcherName, matcherFactory]) => {
2020-03-13 15:44:24 +05:30
// Don't override existing Jest matcher
if (matcherName === 'toHaveLength') {
return;
}
2019-07-07 11:18:12 +05:30
expect.extend({
[matcherName]: matcherFactory().compare,
});
2019-02-15 15:39:39 +05:30
});
2019-09-30 21:07:59 +05:30
2019-10-12 21:52:04 +05:30
expect.extend(customMatchers);
2021-03-08 18:12:59 +05:30
testUtilsConfig.deprecationWarningHandler = (method, message) => {
const ALLOWED_DEPRECATED_METHODS = [
// https://gitlab.com/gitlab-org/gitlab/-/issues/295679
'finding components with `find` or `get`',
// https://gitlab.com/gitlab-org/gitlab/-/issues/295680
'finding components with `findAll`',
];
if (!ALLOWED_DEPRECATED_METHODS.includes(method)) {
global.console.error(message);
}
};
2019-10-12 21:52:04 +05:30
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);
},
});
2019-12-04 20:38:33 +05:30
// make sure that each test actually tests something
// see https://jestjs.io/docs/en/expect#expecthasassertions
beforeEach(() => {
expect.hasAssertions();
});