Bug 1557247 - [devtools] Share node global shims and mocks across panels. r=bomsy.

This patch adds a shared-node-helper function that, for now,
exposes a single setMocksInGlobal function. This function
adds node global missing apis (e.g. requestAnimationFrame, indexedDB, ...),
and is called from the different node setup files, than can then be cleaned up.
For some reason, a debugger test had to be updated to pass.

Differential Revision: https://phabricator.services.mozilla.com/D99471
This commit is contained in:
Nicolas Chevobbe 2021-01-12 07:08:21 +00:00
Родитель ec7fee5e53
Коммит 6bdda30b67
9 изменённых файлов: 125 добавлений и 218 удалений

Просмотреть файл

@ -8,3 +8,8 @@
const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
Enzyme.configure({ adapter: new Adapter() });
const {
setMocksInGlobal,
} = require("devtools/client/shared/test-helpers/shared-node-helpers");
setMocksInGlobal();

Просмотреть файл

@ -4,36 +4,12 @@
"use strict";
/* global global */
// Configure enzyme with React 16 adapter.
const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
Enzyme.configure({ adapter: new Adapter() });
global.loader = {
lazyGetter: (context, name, fn) => {
const module = fn();
global[name] = module;
},
lazyRequireGetter: (context, names, module, destructure) => {
if (!Array.isArray(names)) {
names = [names];
}
for (const name of names) {
const value = destructure
? require(module)[name]
: require(module || name);
global[name] = value;
}
},
};
global.requestIdleCallback = function() {};
// Used for the HTMLTooltip component.
// And set "isSystemPrincipal: false" because can't support XUL element in node.
global.document.nodePrincipal = {
isSystemPrincipal: false,
};
const {
setMocksInGlobal,
} = require("devtools/client/shared/test-helpers/shared-node-helpers");
setMocksInGlobal();

Просмотреть файл

@ -4,46 +4,12 @@
"use strict";
/* global global */
// Configure enzyme with React 16 adapter.
const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
Enzyme.configure({ adapter: new Adapter() });
global.loader = {
lazyGetter: (context, name, fn) => {
const module = fn();
global[name] = module;
},
lazyRequireGetter: (obj, properties, module, destructure) => {
if (!Array.isArray(properties)) {
properties = [properties];
}
for (const property of properties) {
Object.defineProperty(obj, property, {
get: () => {
// Redefine this accessor property as a data property.
// Delete it first, to rule out "too much recursion" in case obj is
// a proxy whose defineProperty handler might unwittingly trigger this
// getter again.
delete obj[property];
const value = destructure
? require(module)[property]
: require(module || property);
Object.defineProperty(obj, property, {
value,
writable: true,
configurable: true,
enumerable: true,
});
return value;
},
configurable: true,
enumerable: true,
});
}
},
lazyImporter: () => {},
};
const {
setMocksInGlobal,
} = require("devtools/client/shared/test-helpers/shared-node-helpers");
setMocksInGlobal();

Просмотреть файл

@ -407,11 +407,8 @@ describe("QuickOpenModal", () => {
});
it("on Enter with results, handle symbol shortcut", () => {
const symbols: Object = [":", "#", "@"];
let key;
let symbol;
for (key in symbols) {
symbol = symbols[key];
const symbols = [":", "#", "@"];
for (const symbol of symbols) {
const { wrapper, props } = generateModal(
{
enabled: true,

Просмотреть файл

@ -2,74 +2,15 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
const {
setMocksInGlobal,
} = require("devtools/client/shared/test-helpers/shared-node-helpers");
setMocksInGlobal();
global.requestAnimationFrame = callback => setTimeout(callback, 0);
global.isWorker = false;
global.define = function() {};
global.loader = {
lazyServiceGetter: () => {},
lazyGetter: (context, name, fn) => {
try {
global[name] = fn();
} catch (_) {}
},
lazyRequireGetter: (context, names, _path, destruct) => {
if (
!_path ||
_path.startsWith("resource://") ||
_path.match(/server\/actors/)
) {
return;
}
const excluded = [
"Debugger",
"devtools/shared/event-emitter",
"devtools/client/shared/autocomplete-popup",
"devtools/client/framework/devtools",
"devtools/client/shared/keycodes",
"devtools/client/shared/sourceeditor/editor",
"devtools/client/shared/telemetry",
"devtools/client/shared/save-screenshot",
"devtools/client/shared/focus",
];
if (!excluded.includes(_path)) {
if (!Array.isArray(names)) {
names = [names];
}
for (const name of names) {
// $FlowIgnore
const module = require(_path);
global[name] = destruct ? module[name] : module;
}
}
},
};
global.DebuggerConfig = {};
// $FlowIgnore
const { LocalizationHelper } = require("devtools/shared/l10n");
global.L10N = new LocalizationHelper(
"devtools/client/locales/debugger.properties"
);
global.performance = { now: () => 0 };
const { URL } = require("url");
global.URL = URL;
function mockIndexeddDB() {
const store = {};
return {
open: () => ({}),
getItem: async key => store[key],
setItem: async (key, value) => {
store[key] = value;
},
};
}
global.indexedDB = mockIndexeddDB();

Просмотреть файл

@ -4,30 +4,7 @@
"use strict";
/* global global */
global.loader = {
lazyGetter: (context, name, fn) => {
const module = fn();
global[name] = module;
},
lazyRequireGetter: (context, names, module, destructure) => {
if (!Array.isArray(names)) {
names = [names];
}
for (const name of names) {
const value = destructure
? require(module)[name]
: require(module || name);
global[name] = value;
}
},
};
global.define = function(fn) {
fn(null, global, { exports: global });
};
global.requestIdleCallback = function() {};
global.isWorker = false;
const {
setMocksInGlobal,
} = require("devtools/client/shared/test-helpers/shared-node-helpers");
setMocksInGlobal();

Просмотреть файл

@ -4,28 +4,12 @@
"use strict";
/* global global */
// Configure enzyme with React 16 adapter.
const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
Enzyme.configure({ adapter: new Adapter() });
global.loader = {
lazyGetter: (context, name, fn) => {
const module = fn();
global[name] = module;
},
lazyRequireGetter: (context, names, module, destructure) => {
if (!Array.isArray(names)) {
names = [names];
}
for (const name of names) {
const value = destructure
? require(module)[name]
: require(module || name);
global[name] = value;
}
},
};
const {
setMocksInGlobal,
} = require("devtools/client/shared/test-helpers/shared-node-helpers");
setMocksInGlobal();

Просмотреть файл

@ -2,8 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
/* global global */
"use strict";
// Configure enzyme with React 16 adapter.
@ -11,38 +9,7 @@ const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
Enzyme.configure({ adapter: new Adapter() });
global.requestAnimationFrame = function(cb) {
cb();
return null;
};
// Mock getSelection
let selection;
const selectionObject = {
toString: () => selection,
get type() {
if (selection === undefined) {
return "None";
}
if (selection === "") {
return "Caret";
}
return "Range";
},
setMockSelection: str => {
selection = str;
},
};
global.getSelection = () => selectionObject;
// Array#flatMap is only supported in Node 11+
if (!Array.prototype.flatMap) {
// eslint-disable-next-line no-extend-native
Array.prototype.flatMap = function(cb) {
return this.reduce((acc, x, i, arr) => {
return acc.concat(cb(x, i, arr));
}, []);
};
}
const {
setMocksInGlobal,
} = require("devtools/client/shared/test-helpers/shared-node-helpers");
setMocksInGlobal();

Просмотреть файл

@ -0,0 +1,94 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* global global */
/**
* Adds mocks for browser-environment global variables/methods to Node global.
*/
function setMocksInGlobal() {
global.isWorker = false;
global.loader = {
lazyGetter: (context, name, fn) => {
const module = fn();
global[name] = module;
},
lazyRequireGetter: (context, names, module, destructure) => {
if (!Array.isArray(names)) {
names = [names];
}
for (const name of names) {
const value = destructure
? require(module)[name]
: require(module || name);
global[name] = value;
}
},
lazyImporter: () => {},
};
global.define = function() {};
// Used for the HTMLTooltip component.
// And set "isSystemPrincipal: false" because can't support XUL element in node.
global.document.nodePrincipal = {
isSystemPrincipal: false,
};
global.requestIdleCallback = function() {};
global.requestAnimationFrame = function(cb) {
cb();
return null;
};
// Mock getSelection
let selection;
global.getSelection = function() {
return {
toString: () => selection,
get type() {
if (selection === undefined) {
return "None";
}
if (selection === "") {
return "Caret";
}
return "Range";
},
setMockSelection: str => {
selection = str;
},
};
};
// Array#flatMap is only supported in Node 11+
if (!Array.prototype.flatMap) {
// eslint-disable-next-line no-extend-native
Array.prototype.flatMap = function(cb) {
return this.reduce((acc, x, i, arr) => {
return acc.concat(cb(x, i, arr));
}, []);
};
}
global.indexedDB = function() {
const store = {};
return {
open: () => ({}),
getItem: async key => store[key],
setItem: async (key, value) => {
store[key] = value;
},
};
};
}
module.exports = {
setMocksInGlobal,
};