2016-10-10 00:48:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const fs = require("fs");
|
2016-10-11 11:54:17 +00:00
|
|
|
const {createInstrumenter} = require("istanbul-lib-instrument");
|
2016-10-10 00:48:35 +00:00
|
|
|
const {jsdom, createVirtualConsole} = require("jsdom");
|
|
|
|
|
2016-10-11 14:35:50 +00:00
|
|
|
var virtualConsole = createVirtualConsole();
|
|
|
|
|
|
|
|
// Optionally print console logs from the jsdom window.
|
|
|
|
if (process.env.ENABLE_JSDOM_CONSOLE == "y") {
|
|
|
|
virtualConsole.sendTo(console);
|
|
|
|
}
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
// Path to the browser-polyfill script, relative to the current work dir
|
|
|
|
// where mocha is executed.
|
2017-04-12 15:30:33 +00:00
|
|
|
let BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.js";
|
|
|
|
|
|
|
|
if (process.env.TEST_MINIFIED_POLYFILL) {
|
|
|
|
BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.min.js";
|
|
|
|
} else if (process.env.TEST_BUNDLED_POLYFILL) {
|
|
|
|
BROWSER_POLYFILL_PATH = process.env.TEST_BUNDLED_POLYFILL;
|
|
|
|
}
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2016-10-11 11:54:17 +00:00
|
|
|
// Create the jsdom window used to run the tests
|
2016-10-11 14:35:50 +00:00
|
|
|
const testDOMWindow = jsdom("", {virtualConsole}).defaultView;
|
2016-10-11 11:54:17 +00:00
|
|
|
|
2016-10-11 14:35:50 +00:00
|
|
|
// Copy the code coverage of the browser-polyfill script from the jsdom window
|
|
|
|
// to the nodejs global, where nyc expects to find the code coverage data to
|
|
|
|
// render in the reports.
|
|
|
|
after(() => {
|
2016-10-12 00:03:25 +00:00
|
|
|
if (testDOMWindow && process.env.COVERAGE == "y") {
|
|
|
|
global.__coverage__ = testDOMWindow.__coverage__;
|
2016-10-11 14:35:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function setupTestDOMWindow(chromeObject, browserObject = undefined) {
|
2016-10-10 00:48:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-10-11 11:54:17 +00:00
|
|
|
const window = testDOMWindow;
|
2016-10-10 00:48:35 +00:00
|
|
|
|
2019-07-18 13:21:49 +00:00
|
|
|
// Ensure that "chrome.runtime.id" is set, because the polyfill is only
|
|
|
|
// loaded in extension environments.
|
|
|
|
if (chromeObject) {
|
|
|
|
if (!chromeObject.runtime) {
|
|
|
|
chromeObject.runtime = {};
|
|
|
|
}
|
|
|
|
if (!chromeObject.runtime.id) {
|
|
|
|
chromeObject.runtime.id = "some-test-id-from-test-setup";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
// Inject the fake chrome object used as a fixture for the particular
|
|
|
|
// browser-polyfill test scenario.
|
|
|
|
window.chrome = chromeObject;
|
|
|
|
|
2016-10-11 14:35:50 +00:00
|
|
|
// Set (or reset) the browser property.
|
|
|
|
if (browserObject) {
|
2018-08-15 20:33:37 +00:00
|
|
|
// Make the fake browser object a `window.Object` instance, so that
|
|
|
|
// it passes the `Object.getPrototypeOf(browser) !== Object.prototype`
|
|
|
|
// check, otherwise it is going to be overridden by the polyfill (See #153).
|
|
|
|
window.browser = Object.assign(window.Object(), browserObject);
|
2016-10-11 14:35:50 +00:00
|
|
|
} else {
|
2018-06-19 21:36:50 +00:00
|
|
|
delete window.browser;
|
2016-10-11 14:35:50 +00:00
|
|
|
}
|
2016-10-11 11:54:17 +00:00
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
const scriptEl = window.document.createElement("script");
|
|
|
|
|
|
|
|
if (process.env.COVERAGE == "y") {
|
|
|
|
// If the code coverage is enabled, instrument the code on the fly
|
|
|
|
// before executing it in the jsdom window.
|
|
|
|
const inst = createInstrumenter({
|
|
|
|
compact: false, esModules: false, produceSourceMap: false,
|
|
|
|
});
|
|
|
|
const scriptContent = fs.readFileSync(BROWSER_POLYFILL_PATH, "utf-8");
|
|
|
|
scriptEl.textContent = inst.instrumentSync(scriptContent, BROWSER_POLYFILL_PATH);
|
|
|
|
} else {
|
|
|
|
scriptEl.src = BROWSER_POLYFILL_PATH;
|
|
|
|
}
|
|
|
|
|
2016-10-11 11:54:17 +00:00
|
|
|
let onLoad;
|
|
|
|
let onLoadError;
|
|
|
|
let onError;
|
|
|
|
|
|
|
|
let cleanLoadListeners = () => {
|
|
|
|
scriptEl.removeEventListener("load", onLoad);
|
|
|
|
scriptEl.removeEventListener("error", onLoadError);
|
|
|
|
|
|
|
|
window.removeEventListener("error", onError);
|
|
|
|
};
|
|
|
|
|
|
|
|
onLoad = () => { cleanLoadListeners(); resolve(window); };
|
|
|
|
onLoadError = () => {
|
|
|
|
cleanLoadListeners();
|
|
|
|
reject(new Error(`Error loading script: ${BROWSER_POLYFILL_PATH}`));
|
|
|
|
};
|
|
|
|
onError = (err) => { cleanLoadListeners(); reject(err); };
|
|
|
|
|
|
|
|
// Listen to any uncaught errors.
|
|
|
|
window.addEventListener("error", onError);
|
|
|
|
scriptEl.addEventListener("error", onLoadError);
|
|
|
|
|
|
|
|
scriptEl.addEventListener("load", onLoad);
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
window.document.body.appendChild(scriptEl);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
BROWSER_POLYFILL_PATH,
|
|
|
|
setupTestDOMWindow,
|
|
|
|
};
|