2020-09-24 18:09:51 +00:00
|
|
|
/* eslint-disable import/no-commonjs, max-classes-per-file */
|
2019-03-26 19:49:05 +00:00
|
|
|
|
2019-07-18 18:46:46 +00:00
|
|
|
const path = require('path');
|
2019-03-26 19:49:05 +00:00
|
|
|
const { ErrorWithStack } = require('jest-util');
|
2020-10-07 21:08:21 +00:00
|
|
|
const JSDOMEnvironment = require('jest-environment-jsdom');
|
2020-07-10 09:09:01 +00:00
|
|
|
const { TEST_HOST } = require('./helpers/test_constants');
|
2019-03-26 19:49:05 +00:00
|
|
|
|
2019-07-18 18:46:46 +00:00
|
|
|
const ROOT_PATH = path.resolve(__dirname, '../..');
|
|
|
|
|
2019-03-26 19:49:05 +00:00
|
|
|
class CustomEnvironment extends JSDOMEnvironment {
|
|
|
|
constructor(config, context) {
|
2020-07-10 09:09:01 +00:00
|
|
|
// Setup testURL so that window.location is setup properly
|
|
|
|
super({ ...config, testURL: TEST_HOST }, context);
|
2019-03-28 12:38:29 +00:00
|
|
|
|
2019-03-26 19:49:05 +00:00
|
|
|
Object.assign(context.console, {
|
|
|
|
error(...args) {
|
|
|
|
throw new ErrorWithStack(
|
|
|
|
`Unexpected call of console.error() with:\n\n${args.join(', ')}`,
|
|
|
|
this.error,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
warn(...args) {
|
|
|
|
throw new ErrorWithStack(
|
|
|
|
`Unexpected call of console.warn() with:\n\n${args.join(', ')}`,
|
|
|
|
this.warn,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2019-03-28 12:38:29 +00:00
|
|
|
|
|
|
|
const { testEnvironmentOptions } = config;
|
2019-04-25 09:49:06 +00:00
|
|
|
const { IS_EE } = testEnvironmentOptions;
|
2019-03-28 12:38:29 +00:00
|
|
|
this.global.gon = {
|
2019-04-25 09:49:06 +00:00
|
|
|
ee: IS_EE,
|
2019-03-28 12:38:29 +00:00
|
|
|
};
|
2019-12-03 18:06:49 +00:00
|
|
|
this.global.IS_EE = IS_EE;
|
2019-03-25 20:15:25 +00:00
|
|
|
|
|
|
|
this.rejectedPromises = [];
|
|
|
|
|
|
|
|
this.global.promiseRejectionHandler = error => {
|
|
|
|
this.rejectedPromises.push(error);
|
|
|
|
};
|
2019-04-25 09:49:06 +00:00
|
|
|
|
2019-07-17 22:47:33 +00:00
|
|
|
this.global.fixturesBasePath = `${ROOT_PATH}/tmp/tests/frontend/fixtures${IS_EE ? '-ee' : ''}`;
|
2019-07-18 19:11:09 +00:00
|
|
|
this.global.staticFixturesBasePath = `${ROOT_PATH}/spec/frontend/fixtures`;
|
2019-05-09 11:45:37 +00:00
|
|
|
|
2019-10-31 18:06:53 +00: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 09:09:01 +00: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 21:10:15 +00:00
|
|
|
this.global.jsdom = this.dom;
|
2020-07-31 21:10:12 +00:00
|
|
|
|
|
|
|
Object.assign(this.global.performance, {
|
|
|
|
mark: () => null,
|
|
|
|
measure: () => null,
|
|
|
|
getEntriesByName: () => [],
|
|
|
|
});
|
2020-09-24 18:09:51 +00:00
|
|
|
|
|
|
|
this.global.PerformanceObserver = class {
|
|
|
|
/* eslint-disable no-useless-constructor, no-unused-vars, no-empty-function, class-methods-use-this */
|
|
|
|
constructor(callback) {}
|
|
|
|
disconnect() {}
|
|
|
|
observe(element, initObject) {}
|
|
|
|
/* eslint-enable no-useless-constructor, no-unused-vars, no-empty-function, class-methods-use-this */
|
|
|
|
};
|
2019-03-25 20:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async teardown() {
|
|
|
|
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 19:49:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CustomEnvironment;
|