From e494d2dfb0e930382f5de32f59ba5e4a56b8cee3 Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Wed, 19 Aug 2020 13:21:05 +0200 Subject: [PATCH 1/2] [FIX] HTML mode: QUnit.config.noglobals with coverage enabled Code instrumented by istanbul introduces global variables (cov_* for each file and a global __coverage__ variable). This causes a failure when the "noglobals" QUnit config is active and some file is loaded during a test as this pollutes the global namespace. Hooking into the "after" function allows to add those variables to the list of known globals so that they won't be detected as new globals. --- lib/client/browser.js | 24 ++++++++++++++++++ .../webapp/test/test.qunit.js | 25 ++++++++++++++----- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/lib/client/browser.js b/lib/client/browser.js index bc8540d9..7a790e75 100644 --- a/lib/client/browser.js +++ b/lib/client/browser.js @@ -218,6 +218,30 @@ require("./discovery.js"); QUnit.testStart(function(test) { timer = new Date().getTime(); testResult = {success: true, errors: []}; + + // Prevent false-positives when the "noglobals" config is enabled. + // Code instrumented by istanbul introduces global variables + // (cov_* for each file and a global __coverage__ variable). + // Adding them to the already collected "pollution" list prevents issues + // when the original "after" hook compares the list with the current state. + const config = QUnit.config; + if (!config.noglobals) { + return; + } + const currentTest = config.current; + const originalAfter = currentTest.after; + currentTest.after = function(...args) { + for (const key in testWindow.contentWindow) { + if ( + Object.prototype.hasOwnProperty.call(testWindow.contentWindow, key) && + (key.startsWith("cov_") || key === "__coverage__") && + !config.pollution.includes(key) + ) { + config.pollution.push(key); + } + } + return originalAfter.apply(currentTest, args); + }; }); QUnit.log(function(details) { diff --git a/test/integration/application-proxy/webapp/test/test.qunit.js b/test/integration/application-proxy/webapp/test/test.qunit.js index 64b61da5..fb3d5ecb 100644 --- a/test/integration/application-proxy/webapp/test/test.qunit.js +++ b/test/integration/application-proxy/webapp/test/test.qunit.js @@ -5,11 +5,24 @@ QUnit.config.autostart = false; sap.ui.getCore().attachInit(function() { "use strict"; - sap.ui.require(["test/app/foo"], function() { - QUnit.test("Karma", function(assert) { - assert.ok(parent.__karma__.files["/base/webapp/.dotfile"], "Karma files should contain dotfiles"); - }); - - QUnit.start(); + QUnit.test("Karma", function(assert) { + assert.ok(parent.__karma__.files["/base/webapp/.dotfile"], "Karma files should contain dotfiles"); }); + + QUnit.test( + "Loading files during a test should not result into 'Introduced global variable(s)' issues " + + "when QUnit.config.noglobals is active", + function(assert) { + const done = assert.async(); + sap.ui.require(["test/app/foo"], function() { + assert.ok(true, "test/app/foo has been loaded"); + done(); + }, function(err) { + assert.ok(false, "Failed to load test/app/foo: " + err); + }); + } + ); + QUnit.config.noglobals = true; + + QUnit.start(); }); From 63c737530b93aa4e5f5367c1d48c705703561dba Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Thu, 20 Aug 2020 15:36:38 +0200 Subject: [PATCH 2/2] [INTERNAL] Fix IE11 --- lib/client/browser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client/browser.js b/lib/client/browser.js index 7a790e75..dc6d90f0 100644 --- a/lib/client/browser.js +++ b/lib/client/browser.js @@ -234,8 +234,8 @@ require("./discovery.js"); for (const key in testWindow.contentWindow) { if ( Object.prototype.hasOwnProperty.call(testWindow.contentWindow, key) && - (key.startsWith("cov_") || key === "__coverage__") && - !config.pollution.includes(key) + (key.indexOf("cov_") === 0 || key === "__coverage__") && + config.pollution.indexOf(key) === -1 ) { config.pollution.push(key); }