2019-03-26 15:49:05 -04:00
|
|
|
/* eslint-disable import/no-commonjs */
|
|
|
|
|
2019-07-18 14:46:46 -04:00
|
|
|
const path = require('path');
|
2019-03-26 15:49:05 -04:00
|
|
|
const { ErrorWithStack } = require('jest-util');
|
2020-06-16 08:09:00 -04:00
|
|
|
const JSDOMEnvironment = require('jest-environment-jsdom-sixteen');
|
2020-07-10 05:09:01 -04: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
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
|
|
|
this.rejectedPromises = [];
|
|
|
|
|
|
|
|
this.global.promiseRejectionHandler = error => {
|
|
|
|
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
|
|
|
|
|
|
|
Object.assign(this.global.performance, {
|
|
|
|
mark: () => null,
|
|
|
|
measure: () => null,
|
|
|
|
getEntriesByName: () => [],
|
|
|
|
});
|
2019-03-25 16:15:25 -04: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 15:49:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CustomEnvironment;
|