2022-11-08 10:31:22 +01:00
|
|
|
/* eslint-disable-next-line */
|
|
|
|
import { TextEncoder, TextDecoder } from 'util';
|
2021-02-04 19:15:23 +01:00
|
|
|
import nock from 'nock';
|
|
|
|
import log from 'loglevel';
|
|
|
|
import { JSDOM } from 'jsdom';
|
2020-07-18 01:36:29 +02:00
|
|
|
|
2021-09-21 18:28:13 +02:00
|
|
|
process.env.IN_TEST = true;
|
2023-01-17 16:51:35 +01:00
|
|
|
process.env.METAMASK_BUILD_TYPE = 'main';
|
2021-09-21 18:28:13 +02:00
|
|
|
|
2022-08-16 17:21:42 +02:00
|
|
|
global.chrome = {
|
|
|
|
runtime: { id: 'testid', getManifest: () => ({ manifest_version: 2 }) },
|
|
|
|
};
|
2022-03-18 20:07:05 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
nock.disableNetConnect();
|
|
|
|
nock.enableNetConnect('localhost');
|
2022-09-29 18:39:35 +02:00
|
|
|
if (typeof beforeEach === 'function') {
|
|
|
|
/* eslint-disable-next-line jest/require-top-level-describe */
|
|
|
|
beforeEach(() => {
|
|
|
|
nock.cleanAll();
|
|
|
|
});
|
|
|
|
}
|
2018-07-30 15:27:25 +02:00
|
|
|
|
2020-01-27 16:15:37 +01:00
|
|
|
// catch rejections that are still unhandled when tests exit
|
2021-02-04 19:15:23 +01:00
|
|
|
const unhandledRejections = new Map();
|
2020-01-27 16:15:37 +01:00
|
|
|
process.on('unhandledRejection', (reason, promise) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log('Unhandled rejection:', reason);
|
|
|
|
unhandledRejections.set(promise, reason);
|
|
|
|
});
|
2020-01-27 16:15:37 +01:00
|
|
|
process.on('rejectionHandled', (promise) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(`handled: ${unhandledRejections.get(promise)}`);
|
|
|
|
unhandledRejections.delete(promise);
|
|
|
|
});
|
2020-01-27 16:15:37 +01:00
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
if (unhandledRejections.size > 0) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(`Found ${unhandledRejections.size} unhandled rejections:`);
|
2020-01-27 16:15:37 +01:00
|
|
|
for (const reason of unhandledRejections.values()) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error('Unhandled rejection: ', reason);
|
2020-01-27 16:15:37 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
process.exit(1);
|
2020-01-27 16:15:37 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-27 16:15:37 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
log.setDefaultLevel(5);
|
|
|
|
global.log = log;
|
2017-02-21 08:42:40 +01:00
|
|
|
|
2017-03-30 23:43:56 +02:00
|
|
|
//
|
|
|
|
// polyfills
|
|
|
|
//
|
|
|
|
|
2020-04-15 19:23:27 +02:00
|
|
|
// dom
|
2021-02-04 19:15:23 +01:00
|
|
|
const jsdom = new JSDOM();
|
|
|
|
global.window = jsdom.window;
|
2020-04-15 19:23:27 +02:00
|
|
|
|
|
|
|
// required by `trezor-connect/node_modules/whatwg-fetch`
|
2021-02-04 19:15:23 +01:00
|
|
|
global.self = window;
|
2020-04-15 19:23:27 +02:00
|
|
|
// required by `dom-helpers` and various other libraries
|
2021-02-04 19:15:23 +01:00
|
|
|
global.document = window.document;
|
2020-04-15 19:23:27 +02:00
|
|
|
// required by `react-tippy`
|
2021-02-04 19:15:23 +01:00
|
|
|
global.navigator = window.navigator;
|
|
|
|
global.Element = window.Element;
|
2020-05-27 17:31:53 +02:00
|
|
|
// required by `react-popper`
|
2021-02-04 19:15:23 +01:00
|
|
|
global.HTMLElement = window.HTMLElement;
|
2020-05-27 17:31:53 +02:00
|
|
|
|
2022-08-23 22:13:02 +02:00
|
|
|
// Jest no longer adds the following timers so we use set/clear Timeouts
|
|
|
|
global.setImmediate =
|
|
|
|
global.setImmediate || ((fn, ...args) => global.setTimeout(fn, 0, ...args));
|
|
|
|
global.clearImmediate =
|
|
|
|
global.clearImmediate || ((id) => global.clearTimeout(id));
|
|
|
|
|
2020-05-27 17:31:53 +02:00
|
|
|
// required by any components anchored on `popover-content`
|
2021-02-04 19:15:23 +01:00
|
|
|
const popoverContent = window.document.createElement('div');
|
|
|
|
popoverContent.setAttribute('id', 'popover-content');
|
|
|
|
window.document.body.appendChild(popoverContent);
|
2020-04-15 19:23:27 +02:00
|
|
|
|
2018-05-25 07:10:17 +02:00
|
|
|
// fetch
|
2022-07-26 20:10:51 +02:00
|
|
|
// fetch is part of node js in future versions, thus triggering no-shadow
|
|
|
|
// eslint-disable-next-line no-shadow
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetch = require('node-fetch');
|
2020-01-30 02:01:32 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { Headers, Request, Response } = fetch;
|
|
|
|
Object.assign(window, { fetch, Headers, Request, Response });
|
2018-05-25 07:10:17 +02:00
|
|
|
|
2017-03-30 23:43:56 +02:00
|
|
|
// localStorage
|
2020-12-04 00:55:23 +01:00
|
|
|
window.localStorage = {
|
|
|
|
removeItem: () => null,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2022-04-28 14:36:28 +02:00
|
|
|
|
|
|
|
// used for native dark/light mode detection
|
|
|
|
window.matchMedia = () => true;
|
|
|
|
|
2020-08-20 19:48:43 +02:00
|
|
|
// override @metamask/logo
|
2021-02-04 19:15:23 +01:00
|
|
|
window.requestAnimationFrame = () => undefined;
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2017-03-30 23:43:56 +02:00
|
|
|
// crypto.getRandomValues
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!window.crypto) {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.crypto = {};
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
|
|
|
if (!window.crypto.getRandomValues) {
|
2020-10-21 18:31:03 +02:00
|
|
|
// eslint-disable-next-line node/global-require
|
2021-02-04 19:15:23 +01:00
|
|
|
window.crypto.getRandomValues = require('polyfill-crypto.getrandomvalues');
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2022-02-23 21:30:26 +01:00
|
|
|
|
2022-11-08 10:31:22 +01:00
|
|
|
// TextEncoder/TextDecoder
|
|
|
|
window.TextEncoder = TextEncoder;
|
|
|
|
window.TextDecoder = TextDecoder;
|
|
|
|
|
2022-02-23 21:30:26 +01:00
|
|
|
// Used to test `clearClipboard` function
|
|
|
|
if (!window.navigator.clipboard) {
|
|
|
|
window.navigator.clipboard = {};
|
|
|
|
}
|
|
|
|
if (!window.navigator.clipboard.writeText) {
|
|
|
|
window.navigator.clipboard.writeText = () => undefined;
|
|
|
|
}
|