diff --git a/devtools/client/shared/test-helpers/jest-fixtures/indexed-db.js b/devtools/client/shared/test-helpers/jest-fixtures/indexed-db.js new file mode 100644 index 000000000000..32bb957a65e1 --- /dev/null +++ b/devtools/client/shared/test-helpers/jest-fixtures/indexed-db.js @@ -0,0 +1,15 @@ +/* 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 . */ + +"use strict"; + +const store = {}; + +module.exports = { + open: () => ({}), + getItem: async key => store[key], + setItem: async (key, value) => { + store[key] = value; + }, +}; diff --git a/devtools/client/shared/test-helpers/shared-jest.config.js b/devtools/client/shared/test-helpers/shared-jest.config.js index ecd6c807adaf..504a347cf4bd 100644 --- a/devtools/client/shared/test-helpers/shared-jest.config.js +++ b/devtools/client/shared/test-helpers/shared-jest.config.js @@ -33,6 +33,7 @@ module.exports = { // Sometimes returning an empty object is enough "^resource://devtools/client/shared/link": `${fixturesDir}/empty-module`, "^devtools/shared/flags": `${fixturesDir}/empty-module`, + "^resource://devtools/shared/indexed-db.js": `${fixturesDir}/indexed-db`, "^devtools/shared/layout/utils": `${fixturesDir}/empty-module`, "^devtools/client/shared/components/tree/TreeView": `${fixturesDir}/empty-module`, // Map all require("devtools/...") to the real devtools root. diff --git a/devtools/client/shared/test-helpers/shared-node-helpers.js b/devtools/client/shared/test-helpers/shared-node-helpers.js index 5fd686bf66f5..0b202f263abb 100644 --- a/devtools/client/shared/test-helpers/shared-node-helpers.js +++ b/devtools/client/shared/test-helpers/shared-node-helpers.js @@ -46,10 +46,6 @@ function setMocksInGlobal() { global.loader = { lazyGetter: (context, name, fn) => { - if (global.hasOwnProperty(name)) { - return; - } - Object.defineProperty(global, name, { get() { delete global[name]; @@ -119,15 +115,6 @@ function setMocksInGlobal() { }; } - const store = {}; - global.indexedDB = { - open: () => ({}), - getItem: async key => store[key], - setItem: async (key, value) => { - store[key] = value; - }, - }; - if (typeof global.TextEncoder === "undefined") { const { TextEncoder } = require("util"); global.TextEncoder = TextEncoder; diff --git a/devtools/shared/indexed-db.js b/devtools/shared/indexed-db.js index a4ea57c76f5c..25cb858142b4 100644 --- a/devtools/shared/indexed-db.js +++ b/devtools/shared/indexed-db.js @@ -9,46 +9,41 @@ * a principal dedicated to DevTools. */ -// When running in jest, we can't use Cu.Sandbox and only expose the native indexedDB object -if (globalThis.indexedDB) { - module.exports = globalThis.indexedDB; -} else { - const PSEUDOURI = "indexeddb://fx-devtools"; - const principaluri = Services.io.newURI(PSEUDOURI); - const principal = Services.scriptSecurityManager.createContentPrincipal( - principaluri, - {} - ); +const PSEUDOURI = "indexeddb://fx-devtools"; +const principaluri = Services.io.newURI(PSEUDOURI); +const principal = Services.scriptSecurityManager.createContentPrincipal( + principaluri, + {} +); - // indexedDB is only exposed to document globals. - // We are retrieving an instance from a Sandbox, which has to be loaded - // from the system principal in order to avoid having wrappers around - // all indexed DB objects. - const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal(); - const sandbox = Cu.Sandbox(systemPrincipal, { - wantGlobalProperties: ["indexedDB"], - }); - const { indexedDB } = sandbox; +// indexedDB is only exposed to document globals. +// We are retrieving an instance from a Sandbox, which has to be loaded +// from the system principal in order to avoid having wrappers around +// all indexed DB objects. +const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal(); +const sandbox = Cu.Sandbox(systemPrincipal, { + wantGlobalProperties: ["indexedDB"], +}); +const { indexedDB } = sandbox; - module.exports = Object.freeze({ - /** - * Only the standard version of indexedDB.open is supported. - */ - open(name, version) { - const options = {}; - if (typeof version === "number") { - options.version = version; - } - return indexedDB.openForPrincipal(principal, name, options); - }, +module.exports = Object.freeze({ + /** + * Only the standard version of indexedDB.open is supported. + */ + open(name, version) { + const options = {}; + if (typeof version === "number") { + options.version = version; + } + return indexedDB.openForPrincipal(principal, name, options); + }, - /** - * Only the standard version of indexedDB.deleteDatabase is supported. - */ - deleteDatabase(name) { - return indexedDB.deleteForPrincipal(principal, name); - }, + /** + * Only the standard version of indexedDB.deleteDatabase is supported. + */ + deleteDatabase(name) { + return indexedDB.deleteForPrincipal(principal, name); + }, - cmp: indexedDB.cmp.bind(indexedDB), - }); -} + cmp: indexedDB.cmp.bind(indexedDB), +});