From 92ccea2e155dbbffb3b20bbe0dbe9f21e26295da Mon Sep 17 00:00:00 2001 From: Luca Greco Date: Tue, 11 Oct 2016 16:35:50 +0200 Subject: [PATCH] test: tweaks on test helpers (support for 'existent browser global' test). --- test/setup.js | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/test/setup.js b/test/setup.js index 1ee8e94..e17c9db 100644 --- a/test/setup.js +++ b/test/setup.js @@ -4,17 +4,31 @@ const fs = require("fs"); const {createInstrumenter} = require("istanbul-lib-instrument"); const {jsdom, createVirtualConsole} = require("jsdom"); -var virtualConsole = createVirtualConsole().sendTo(console); +var virtualConsole = createVirtualConsole(); + +// Optionally print console logs from the jsdom window. +if (process.env.ENABLE_JSDOM_CONSOLE == "y") { + virtualConsole.sendTo(console); +} // Path to the browser-polyfill script, relative to the current work dir // 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; +const testDOMWindow = jsdom("", {virtualConsole}).defaultView; global.window = testDOMWindow; -function setupTestDOMWindow(chromeObject) { +// 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(() => { + if (global.window && process.env.COVERAGE == "y") { + global.__coverage__ = global.window.__coverage__; + } +}); + +function setupTestDOMWindow(chromeObject, browserObject = undefined) { return new Promise((resolve, reject) => { const window = testDOMWindow; @@ -22,9 +36,13 @@ function setupTestDOMWindow(chromeObject) { // 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; + // Set (or reset) the browser property. + if (browserObject) { + window.browser = browserObject; + } else { + // TODO: change into `delete window.browser` once tmpvar/jsdom#1622 has been fixed. + window.browser = undefined; + } const scriptEl = window.document.createElement("script"); @@ -68,15 +86,6 @@ function setupTestDOMWindow(chromeObject) { }); } -// 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(() => { - if (global.window && process.env.COVERAGE == "y") { - global.__coverage__ = global.window.__coverage__; - } -}); - module.exports = { BROWSER_POLYFILL_PATH, setupTestDOMWindow,