Skip to content

Commit

Permalink
[FIX] HTML mode: QUnit.config.noglobals with coverage enabled (#223)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matz3 committed Aug 24, 2020
1 parent 6656709 commit 3764e9f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
24 changes: 24 additions & 0 deletions lib/client/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 19 additions & 6 deletions test/integration/application-proxy/webapp/test/test.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 3764e9f

Please sign in to comment.