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

69 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-02-20 20:26:44 +00:00
import Vue from 'vue';
2019-04-01 18:55:57 +00:00
import * as jqueryMatchers from 'custom-jquery-matchers';
2019-06-22 03:35:51 +00:00
import $ from 'jquery';
2019-02-20 20:26:44 +00:00
import Translate from '~/vue_shared/translate';
2019-02-20 19:59:44 +00:00
import axios from '~/lib/utils/axios_utils';
import { initializeTestTimeout } from './helpers/timeout';
2019-05-15 12:18:54 +00:00
import { loadHTMLFixture, setHTMLFixture } from './helpers/fixtures';
2019-02-20 19:59:44 +00:00
2019-06-22 03:35:51 +00: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-ee/issues/12448
window.jQuery = $;
process.on('unhandledRejection', global.promiseRejectionHandler);
2019-03-25 20:32:48 +00: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 17:05:21 +00:00
initializeTestTimeout(process.env.CI ? 5000 : 500);
2019-02-20 19:59:44 +00:00
// fail tests for unmocked requests
beforeEach(done => {
axios.defaults.adapter = config => {
const error = new Error(`Unexpected unmocked request: ${JSON.stringify(config, null, 2)}`);
error.config = config;
done.fail(error);
return Promise.reject(error);
};
done();
});
2019-02-20 20:26:44 +00:00
2019-03-28 13:17:52 +00:00
Vue.config.devtools = false;
Vue.config.productionTip = false;
2019-02-20 20:26:44 +00:00
Vue.use(Translate);
2019-03-28 19:42:32 +00:00
// workaround for JSDOM not supporting innerText
// see https://github.com/jsdom/jsdom/issues/1245
Object.defineProperty(global.Element.prototype, 'innerText', {
get() {
return this.textContent;
},
configurable: true, // make it so that it doesn't blow chunks on re-running tests with things like --watch
});
// convenience wrapper for migration from Karma
Object.assign(global, {
loadFixtures: loadHTMLFixture,
setFixtures: setHTMLFixture,
2019-05-15 12:18:54 +00: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 18:55:57 +00:00
// custom-jquery-matchers was written for an old Jest version, we need to make it compatible
Object.entries(jqueryMatchers).forEach(([matcherName, matcherFactory]) => {
expect.extend({
[matcherName]: matcherFactory().compare,
});
});