2020-09-24 14:09:51 -04:00
|
|
|
/* eslint-disable import/no-commonjs, max-classes-per-file */
|
2019-03-26 15:49:05 -04:00
|
|
|
|
2019-07-18 14:46:46 -04:00
|
|
|
const path = require('path');
|
2020-10-07 17:08:21 -04:00
|
|
|
const JSDOMEnvironment = require('jest-environment-jsdom');
|
2021-02-14 13:09:20 -05:00
|
|
|
const { ErrorWithStack } = require('jest-util');
|
2021-02-11 22:08:53 -05:00
|
|
|
const {
|
|
|
|
setGlobalDateToFakeDate,
|
|
|
|
setGlobalDateToRealDate,
|
|
|
|
} = require('./__helpers__/fake_date/fake_date');
|
2021-02-14 13:09:20 -05:00
|
|
|
const { TEST_HOST } = require('./__helpers__/test_constants');
|
2019-03-26 15:49:05 -04:00
|
|
|
|
2019-07-18 14:46:46 -04:00
|
|
|
const ROOT_PATH = path.resolve(__dirname, '../..');
|
|
|
|
|
2019-03-26 15:49:05 -04:00
|
|
|
class CustomEnvironment extends JSDOMEnvironment {
|
|
|
|
constructor(config, context) {
|
2020-07-10 05:09:01 -04:00
|
|
|
// Setup testURL so that window.location is setup properly
|
|
|
|
super({ ...config, testURL: TEST_HOST }, context);
|
2019-03-28 08:38:29 -04:00
|
|
|
|
2021-02-10 10:11:19 -05:00
|
|
|
// Fake the `Date` for `jsdom` which fixes things like document.cookie
|
|
|
|
// https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39496#note_503084332
|
|
|
|
setGlobalDateToFakeDate();
|
|
|
|
|
2019-03-26 15:49:05 -04:00
|
|
|
Object.assign(context.console, {
|
|
|
|
error(...args) {
|
|
|
|
throw new ErrorWithStack(
|
|
|
|
`Unexpected call of console.error() with:\n\n${args.join(', ')}`,
|
|
|
|
this.error,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
warn(...args) {
|
2022-02-14 07:14:02 -05:00
|
|
|
if (args[0].includes('The updateQuery callback for fetchMore is deprecated')) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-26 15:49:05 -04:00
|
|
|
throw new ErrorWithStack(
|
|
|
|
`Unexpected call of console.warn() with:\n\n${args.join(', ')}`,
|
|
|
|
this.warn,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2019-03-28 08:38:29 -04:00
|
|
|
|
|
|
|
const { testEnvironmentOptions } = config;
|
2019-04-25 05:49:06 -04:00
|
|
|
const { IS_EE } = testEnvironmentOptions;
|
2019-03-28 08:38:29 -04:00
|
|
|
this.global.gon = {
|
2019-04-25 05:49:06 -04:00
|
|
|
ee: IS_EE,
|
2019-03-28 08:38:29 -04:00
|
|
|
};
|
2019-12-03 13:06:49 -05:00
|
|
|
this.global.IS_EE = IS_EE;
|
2019-03-25 16:15:25 -04:00
|
|
|
|
2021-07-14 20:10:03 -04:00
|
|
|
// Set up global `gl` object
|
|
|
|
this.global.gl = {};
|
|
|
|
|
2019-03-25 16:15:25 -04:00
|
|
|
this.rejectedPromises = [];
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
this.global.promiseRejectionHandler = (error) => {
|
2019-03-25 16:15:25 -04:00
|
|
|
this.rejectedPromises.push(error);
|
|
|
|
};
|
2019-04-25 05:49:06 -04:00
|
|
|
|
2019-07-17 18:47:33 -04:00
|
|
|
this.global.fixturesBasePath = `${ROOT_PATH}/tmp/tests/frontend/fixtures${IS_EE ? '-ee' : ''}`;
|
2019-07-18 15:11:09 -04:00
|
|
|
this.global.staticFixturesBasePath = `${ROOT_PATH}/spec/frontend/fixtures`;
|
2019-05-09 07:45:37 -04:00
|
|
|
|
2019-10-31 14:06:53 -04:00
|
|
|
/**
|
|
|
|
* window.fetch() is required by the apollo-upload-client library otherwise
|
|
|
|
* a ReferenceError is generated: https://github.com/jaydenseric/apollo-upload-client/issues/100
|
|
|
|
*/
|
|
|
|
this.global.fetch = () => {};
|
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
// Expose the jsdom (created in super class) to the global so that we can call reconfigure({ url: '' }) to properly set `window.location`
|
2020-08-06 17:10:15 -04:00
|
|
|
this.global.jsdom = this.dom;
|
2020-07-31 17:10:12 -04:00
|
|
|
|
2021-07-06 11:08:14 -04:00
|
|
|
//
|
|
|
|
// Monaco-related environment variables
|
|
|
|
//
|
|
|
|
Object.defineProperty(this.global, 'matchMedia', {
|
|
|
|
writable: true,
|
|
|
|
value: (query) => ({
|
|
|
|
matches: false,
|
|
|
|
media: query,
|
|
|
|
onchange: null,
|
|
|
|
addListener: () => null, // deprecated
|
|
|
|
removeListener: () => null, // deprecated
|
|
|
|
addEventListener: () => null,
|
|
|
|
removeEventListener: () => null,
|
|
|
|
dispatchEvent: () => null,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2021-08-03 08:09:42 -04:00
|
|
|
/**
|
|
|
|
* JSDom doesn't have an own observer implementation, so this a Noop Observer.
|
|
|
|
* If you are testing functionality, related to observers, have a look at __helpers__/mock_dom_observer.js
|
|
|
|
*
|
|
|
|
* JSDom actually implements a _proper_ MutationObserver, so no need to mock it!
|
|
|
|
*/
|
|
|
|
class NoopObserver {
|
2020-09-24 14:09:51 -04:00
|
|
|
/* eslint-disable no-useless-constructor, no-unused-vars, no-empty-function, class-methods-use-this */
|
|
|
|
constructor(callback) {}
|
|
|
|
disconnect() {}
|
|
|
|
observe(element, initObject) {}
|
2021-08-03 08:09:42 -04:00
|
|
|
unobserve(element) {}
|
|
|
|
takeRecords() {
|
|
|
|
return [];
|
|
|
|
}
|
2020-09-24 14:09:51 -04:00
|
|
|
/* eslint-enable no-useless-constructor, no-unused-vars, no-empty-function, class-methods-use-this */
|
2021-08-03 08:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
['IntersectionObserver', 'PerformanceObserver', 'ResizeObserver'].forEach((observer) => {
|
|
|
|
if (this.global[observer]) {
|
|
|
|
throw new Error(
|
|
|
|
`We overwrite an existing Observer in jsdom (${observer}), are you sure you want to do that?`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.global[observer] = NoopObserver;
|
|
|
|
});
|
2019-03-25 16:15:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async teardown() {
|
2021-02-11 22:08:53 -05:00
|
|
|
// Reset `Date` so that Jest can report timing accurately *roll eyes*...
|
|
|
|
setGlobalDateToRealDate();
|
|
|
|
|
2022-02-22 10:18:06 -05:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2019-03-25 16:15:25 -04:00
|
|
|
await new Promise(setImmediate);
|
|
|
|
|
|
|
|
if (this.rejectedPromises.length > 0) {
|
|
|
|
throw new ErrorWithStack(
|
|
|
|
`Unhandled Promise rejections: ${this.rejectedPromises.join(', ')}`,
|
|
|
|
this.teardown,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await super.teardown();
|
2019-03-26 15:49:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CustomEnvironment;
|