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

144 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
/* eslint-disable import/no-commonjs, max-classes-per-file */
2019-07-07 11:18:12 +05:30
2019-10-12 21:52:04 +05:30
const path = require('path');
2023-03-04 22:38:38 +05:30
const { TestEnvironment } = require('jest-environment-jsdom');
2021-03-11 19:13:27 +05:30
const { ErrorWithStack } = require('jest-util');
const {
setGlobalDateToFakeDate,
setGlobalDateToRealDate,
} = require('./__helpers__/fake_date/fake_date');
2021-03-08 18:12:59 +05:30
const { TEST_HOST } = require('./__helpers__/test_constants');
2023-05-27 22:25:52 +05:30
const { createGon } = require('./__helpers__/gon_helper');
2019-07-07 11:18:12 +05:30
2019-10-12 21:52:04 +05:30
const ROOT_PATH = path.resolve(__dirname, '../..');
2023-03-04 22:38:38 +05:30
class CustomEnvironment extends TestEnvironment {
constructor({ globalConfig, projectConfig }, context) {
2020-07-28 23:09:34 +05:30
// Setup testURL so that window.location is setup properly
2023-03-04 22:38:38 +05:30
super({ globalConfig, projectConfig: { ...projectConfig, testURL: TEST_HOST } }, context);
2019-07-07 11:18:12 +05:30
2021-03-11 19:13:27 +05:30
// Fake the `Date` for `jsdom` which fixes things like document.cookie
// https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39496#note_503084332
setGlobalDateToFakeDate();
2023-06-20 00:43:36 +05:30
const { error: originalErrorFn } = context.console;
2019-07-07 11:18:12 +05:30
Object.assign(context.console, {
error(...args) {
2023-06-20 00:43:36 +05:30
if (
args?.[0]?.includes('[Vue warn]: Missing required prop') ||
args?.[0]?.includes('[Vue warn]: Invalid prop')
) {
originalErrorFn.apply(context.console, args);
return;
}
2019-07-07 11:18:12 +05:30
throw new ErrorWithStack(
`Unexpected call of console.error() with:\n\n${args.join(', ')}`,
this.error,
);
},
warn(...args) {
2023-06-20 00:43:36 +05:30
if (args?.[0]?.includes('The updateQuery callback for fetchMore is deprecated')) {
2022-04-04 11:22:00 +05:30
return;
}
2019-07-07 11:18:12 +05:30
throw new ErrorWithStack(
`Unexpected call of console.warn() with:\n\n${args.join(', ')}`,
this.warn,
);
},
});
2023-03-04 22:38:38 +05:30
const { IS_EE } = projectConfig.testEnvironmentOptions;
2023-05-27 22:25:52 +05:30
2020-01-01 13:55:28 +05:30
this.global.IS_EE = IS_EE;
2019-07-07 11:18:12 +05:30
2023-05-27 22:25:52 +05:30
// Set up global `gon` object
this.global.gon = createGon(IS_EE);
2021-09-30 23:02:18 +05:30
// Set up global `gl` object
this.global.gl = {};
2019-07-07 11:18:12 +05:30
this.rejectedPromises = [];
2021-03-08 18:12:59 +05:30
this.global.promiseRejectionHandler = (error) => {
2019-07-07 11:18:12 +05:30
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;
2021-09-30 23:02:18 +05:30
//
// 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-10-27 15:23:28 +05:30
/**
* 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 {
2021-01-03 14:25:43 +05:30
/* eslint-disable no-useless-constructor, no-unused-vars, no-empty-function, class-methods-use-this */
constructor(callback) {}
disconnect() {}
observe(element, initObject) {}
2021-10-27 15:23:28 +05:30
unobserve(element) {}
takeRecords() {
return [];
}
2021-01-03 14:25:43 +05:30
/* eslint-enable no-useless-constructor, no-unused-vars, no-empty-function, class-methods-use-this */
2021-10-27 15:23:28 +05:30
}
['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-07-07 11:18:12 +05:30
}
async teardown() {
2021-03-11 19:13:27 +05:30
// Reset `Date` so that Jest can report timing accurately *roll eyes*...
setGlobalDateToRealDate();
2022-05-07 20:08:51 +05:30
// eslint-disable-next-line no-restricted-syntax
2019-07-07 11:18:12 +05:30
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;