diff --git a/lib/client/browser.js b/lib/client/browser.js index bc8540d9..dc6d90f0 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.indexOf("cov_") === 0 || key === "__coverage__") && + config.pollution.indexOf(key) === -1 + ) { + 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(); });