fix: cleanup tests setup function.
This commit is contained in:
parent
f2c82fcbea
commit
aaca860c2d
|
@ -1,7 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const {createInstrumenter} = require("istanbul-lib-instrument");
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
const {createInstrumenter} = require("istanbul-lib-instrument");
|
||||||
const {jsdom, createVirtualConsole} = require("jsdom");
|
const {jsdom, createVirtualConsole} = require("jsdom");
|
||||||
|
|
||||||
var virtualConsole = createVirtualConsole().sendTo(console);
|
var virtualConsole = createVirtualConsole().sendTo(console);
|
||||||
|
@ -10,26 +10,22 @@ var virtualConsole = createVirtualConsole().sendTo(console);
|
||||||
// where mocha is executed.
|
// where mocha is executed.
|
||||||
const BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.js";
|
const BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.js";
|
||||||
|
|
||||||
|
// Create the jsdom window used to run the tests
|
||||||
|
const testDOMWindow = jsdom({virtualConsole}).defaultView;
|
||||||
|
global.window = testDOMWindow;
|
||||||
|
|
||||||
function setupTestDOMWindow(chromeObject) {
|
function setupTestDOMWindow(chromeObject) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let window;
|
const window = testDOMWindow;
|
||||||
|
|
||||||
// If a jsdom window has already been created, reuse it so that
|
|
||||||
// we can retrieve the final code coverage data, which has been
|
|
||||||
// collected in the jsdom window (where the instrumented browser-polyfill
|
|
||||||
// is running).
|
|
||||||
if (global.window) {
|
|
||||||
window = global.window;
|
|
||||||
window.browser = undefined;
|
|
||||||
} else {
|
|
||||||
window = jsdom({virtualConsole}).defaultView;
|
|
||||||
global.window = window;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Inject the fake chrome object used as a fixture for the particular
|
// Inject the fake chrome object used as a fixture for the particular
|
||||||
// browser-polyfill test scenario.
|
// browser-polyfill test scenario.
|
||||||
window.chrome = chromeObject;
|
window.chrome = chromeObject;
|
||||||
|
|
||||||
|
// Set the browser property to undefined.
|
||||||
|
// TODO: change into `delete window.browser` once tmpvar/jsdom#1622 has been fixed.
|
||||||
|
window.browser = undefined;
|
||||||
|
|
||||||
const scriptEl = window.document.createElement("script");
|
const scriptEl = window.document.createElement("script");
|
||||||
|
|
||||||
if (process.env.COVERAGE == "y") {
|
if (process.env.COVERAGE == "y") {
|
||||||
|
@ -44,19 +40,31 @@ function setupTestDOMWindow(chromeObject) {
|
||||||
scriptEl.src = BROWSER_POLYFILL_PATH;
|
scriptEl.src = BROWSER_POLYFILL_PATH;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare to listen for script loading errors (which results in a rejection),
|
let onLoad;
|
||||||
// and to detect when the browser-polyfill has been executed (which resolves
|
let onLoadError;
|
||||||
// to the jsdom window where the loading has been completed).
|
let onError;
|
||||||
window.__browserPolyfillLoaded__ = {resolve, reject};
|
|
||||||
window.addEventListener("error", (evt) => reject(evt));
|
let cleanLoadListeners = () => {
|
||||||
const loadedScriptEl = window.document.createElement("script");
|
scriptEl.removeEventListener("load", onLoad);
|
||||||
loadedScriptEl.textContent = `
|
scriptEl.removeEventListener("error", onLoadError);
|
||||||
window.removeEventListener("error", window.__browserPolyfillLoaded__.reject);
|
|
||||||
window.__browserPolyfillLoaded__.resolve(window);
|
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);
|
||||||
|
|
||||||
window.document.body.appendChild(scriptEl);
|
window.document.body.appendChild(scriptEl);
|
||||||
window.document.body.appendChild(loadedScriptEl);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue