2019-03-26 15:49:05 -04:00
|
|
|
/* eslint-disable import/no-commonjs */
|
|
|
|
|
|
|
|
const { ErrorWithStack } = require('jest-util');
|
|
|
|
const JSDOMEnvironment = require('jest-environment-jsdom');
|
|
|
|
|
|
|
|
class CustomEnvironment extends JSDOMEnvironment {
|
|
|
|
constructor(config, context) {
|
|
|
|
super(config, 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-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
|
|
|
|
|
|
|
this.global.fixturesBasePath = `${process.cwd()}/${
|
|
|
|
IS_EE ? 'ee/' : ''
|
|
|
|
}spec/javascripts/fixtures`;
|
2019-05-09 07:45:37 -04:00
|
|
|
|
|
|
|
// Not yet supported by JSDOM: https://github.com/jsdom/jsdom/issues/317
|
|
|
|
this.global.document.createRange = () => ({
|
|
|
|
setStart: () => {},
|
|
|
|
setEnd: () => {},
|
|
|
|
commonAncestorContainer: {
|
|
|
|
nodeName: 'BODY',
|
|
|
|
ownerDocument: this.global.document,
|
|
|
|
},
|
|
|
|
});
|
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;
|