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

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
/* eslint-disable import/no-commonjs */
2019-10-12 21:52:04 +05:30
const path = require('path');
2019-07-07 11:18:12 +05:30
const { ErrorWithStack } = require('jest-util');
2020-06-23 00:09:42 +05:30
const JSDOMEnvironment = require('jest-environment-jsdom-sixteen');
2020-07-28 23:09:34 +05:30
const { TEST_HOST } = require('./helpers/test_constants');
2019-07-07 11:18:12 +05:30
2019-10-12 21:52:04 +05:30
const ROOT_PATH = path.resolve(__dirname, '../..');
2019-07-07 11:18:12 +05:30
class CustomEnvironment extends JSDOMEnvironment {
constructor(config, context) {
2020-07-28 23:09:34 +05:30
// Setup testURL so that window.location is setup properly
super({ ...config, testURL: TEST_HOST }, context);
2019-07-07 11:18:12 +05:30
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,
);
},
});
const { testEnvironmentOptions } = config;
2019-07-31 22:56:46 +05:30
const { IS_EE } = testEnvironmentOptions;
2019-07-07 11:18:12 +05:30
this.global.gon = {
2019-07-31 22:56:46 +05:30
ee: IS_EE,
2019-07-07 11:18:12 +05:30
};
2020-01-01 13:55:28 +05:30
this.global.IS_EE = IS_EE;
2019-07-07 11:18:12 +05:30
this.rejectedPromises = [];
this.global.promiseRejectionHandler = error => {
this.rejectedPromises.push(error);
};
2019-07-31 22:56:46 +05:30
2019-10-12 21:52:04 +05:30
this.global.fixturesBasePath = `${ROOT_PATH}/tmp/tests/frontend/fixtures${IS_EE ? '-ee' : ''}`;
this.global.staticFixturesBasePath = `${ROOT_PATH}/spec/frontend/fixtures`;
2019-09-04 21:01:54 +05:30
2019-12-26 22:10:19 +05:30
/**
* 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-28 23:09:34 +05:30
// Expose the jsdom (created in super class) to the global so that we can call reconfigure({ url: '' }) to properly set `window.location`
2020-10-24 23:57:45 +05:30
this.global.jsdom = this.dom;
Object.assign(this.global.performance, {
mark: () => null,
measure: () => null,
getEntriesByName: () => [],
});
2019-07-07 11:18:12 +05:30
}
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();
}
}
module.exports = CustomEnvironment;