From aaca860c2d944ee36866255408aac9cc97823102 Mon Sep 17 00:00:00 2001 From: Luca Greco Date: Tue, 11 Oct 2016 13:54:17 +0200 Subject: [PATCH] fix: cleanup tests setup function. --- test/setup.js | 58 +++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/test/setup.js b/test/setup.js index 8f4b4c8..1ee8e94 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,7 +1,7 @@ "use strict"; -const {createInstrumenter} = require("istanbul-lib-instrument"); const fs = require("fs"); +const {createInstrumenter} = require("istanbul-lib-instrument"); const {jsdom, createVirtualConsole} = require("jsdom"); var virtualConsole = createVirtualConsole().sendTo(console); @@ -10,26 +10,22 @@ var virtualConsole = createVirtualConsole().sendTo(console); // where mocha is executed. 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) { return new Promise((resolve, reject) => { - let window; - - // 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; - } + const window = testDOMWindow; // Inject the fake chrome object used as a fixture for the particular // browser-polyfill test scenario. 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"); if (process.env.COVERAGE == "y") { @@ -44,19 +40,31 @@ function setupTestDOMWindow(chromeObject) { scriptEl.src = BROWSER_POLYFILL_PATH; } - // Prepare to listen for script loading errors (which results in a rejection), - // and to detect when the browser-polyfill has been executed (which resolves - // to the jsdom window where the loading has been completed). - window.__browserPolyfillLoaded__ = {resolve, reject}; - window.addEventListener("error", (evt) => reject(evt)); - const loadedScriptEl = window.document.createElement("script"); - loadedScriptEl.textContent = ` - window.removeEventListener("error", window.__browserPolyfillLoaded__.reject); - window.__browserPolyfillLoaded__.resolve(window); - `; + 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); window.document.body.appendChild(scriptEl); - window.document.body.appendChild(loadedScriptEl); }); }