Skip to content

Commit

Permalink
[FIX] Missing testrunner.html when using npm dependencies (#27)
Browse files Browse the repository at this point in the history
When using the UI5 Tooling approach there was a problem that the required testrunner.html file was not available as it is part of the test-resources of the sap.ui.core library which are excluded from the npm package content.

This change now integrates the testrunner discovery script into the plugin to remove this dependency and make it independent of UI5.
  • Loading branch information
romaniam authored and matz3 committed Mar 20, 2019
1 parent 52d3030 commit c2adeed
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 119 deletions.
3 changes: 2 additions & 1 deletion lib/client/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
"browser": true
},
"globals": {
"sap": "readonly"
"sap": "readonly",
"karma": "readonly"
}
}
88 changes: 38 additions & 50 deletions lib/client/browser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Export istanbul-lib-coverage to be bundled with webpack for browser usage
const istanbulLibCoverage = require("istanbul-lib-coverage");
require("./discovery.js");

(function(window) {
const karma = window.__karma__;

karma.start = function() {
const config = karma.config && karma.config.ui5 || {};

const prependBase = (path) => /^\/base\//.test(path) ? path : `/base/${path}`;
const prependBase = function(path) {
return /^\/base\//.test(path) ? path : "/base/" + path;
};

const windowUtil = function(url, onload) {
const context = {
Expand Down Expand Up @@ -39,65 +41,51 @@ const istanbulLibCoverage = require("istanbul-lib-coverage");
karma.setupContext(context.contentWindow);
};

if (!config.testrunner) {
karma.log("error", ["No testrunner URL configured."]);
return;
}

if (config.testrunner) {
if (!config.testpage) {
const aTestsuites = getTestsuites(karma.files);

if (aTestsuites.length === 0) {
karma.log("error", [
"Could not find a testsuite or no testpage defined.\n"
+ "Please set a testpage in the config or via CLI.\n"
+ "For more details: https://github.com/SAP/karma-ui5#defining-testpage"]
);
return;
}
if (!config.testpage) {
const aTestsuites = getTestsuites(karma.files);

if (aTestsuites.length !== 1) {
karma.log("error", [
"Multiple testsuites have been found in your project."
+ "Execution of multiple testsuites is not allowed.\n"
+ "Please explicitly define a testpage in your karma config or via CLI.\n"
+ "For reference: https://github.com/SAP/karma-ui5#defining-testpage"]);
return;
}
if (aTestsuites.length === 0) {
karma.log("error", [
"Could not find a testsuite or no testpage defined.\n"
+ "Please set a testpage in the config or via CLI.\n"
+ "For more details: https://github.com/SAP/karma-ui5#defining-testpage"]
);
return;
}

config.testpage = aTestsuites[0];
if (aTestsuites.length !== 1) {
karma.log("error", [
"Multiple testsuites have been found in your project."
+ "Execution of multiple testsuites is not allowed.\n"
+ "Please explicitly define a testpage in your karma config or via CLI.\n"
+ "For reference: https://github.com/SAP/karma-ui5#defining-testpage"]);
return;
}

config.testrunner = prependBase(config.testrunner);
config.testpage = prependBase(config.testpage);
config.testpage = aTestsuites[0];
}

windowUtil(config.testrunner, function(testRunner) {
if (window.top) {
window.top.jsUnitTestSuite = testRunner.contentWindow.jsUnitTestSuite;
} else {
window.jsUnitTestSuite = testRunner.contentWindow.jsUnitTestSuite;
}
config.testpage = prependBase(config.testpage);

testRunner.contentWindow.sap.ui.qunit.TestRunner.checkTestPage(config.testpage)
.then(function(testpages) {
testRunner.close();
runTests(testpages);
}, function(e) {
// console.error("fail");
karma.log("error", [e.message]);
return;
// TODO: report error
});
});
}
window.findTests(config.testpage).then(function(testpages) {
runTests(testpages.map(function(testpage) {
return testpage["fullpage"];
}));
}, function(e) {
// console.error("fail");
karma.log("error", e.message);
// TODO: report error
});

function getTestsuites(files) {
return Object.keys(files).filter((path) => path.endsWith(".qunit.html") && path.includes("/testsuite."));
return Object.keys(files).filter(function(path) {
return /\.qunit\.html$/.test(path) && /\/testsuite\./.test(path);
});
}

function runTests(testpages) {
let totalNumberOfTest = 0; let coverageMap;
let totalNumberOfTest = 0;
let coverageMap;

function mergeCoverage(coverage) {
if (!coverage) {
Expand Down
200 changes: 200 additions & 0 deletions lib/client/discovery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
const Promise = require("es6-promise").Promise;
const assign = require("lodash.assign");

(function(window) {
/*
* Simulate the same JSUnit Testsuite API as the TestRunner to collect the available test pages per suite
*/
function TestSuite() {
this.aPages = [];
}

TestSuite.prototype.getTestPages = function() {
return this.aPages;
};

TestSuite.prototype.addTestPage = function(sTestPage, oConfig) {
// in case of running in the root context the testsuites right now
// generate an invalid URL because they assume that test-resources is
// the context path - this section makes sure to remove the duplicate
// test-resources segments in the path
if ( /^(\/test-resources\/(test-)?resources)/.test(sTestPage) ) {
sTestPage = sTestPage.slice("/test-resources".length);
}
this.aPages.push(assign({fullpage: sTestPage}, oConfig));
};

function getFile(sUrl) {
return new Promise(function(resolve, reject) {
const request = new XMLHttpRequest();
request.open("GET", sUrl, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
resolve(request.responseText);
} else {
reject(new Error("Request failed"));
}
};

request.onerror = function(err) {
reject(err);
};

request.send();
});
}

function findPages(sEntryPage, progressCallback) {
function checkTestPage(oTestPageConfig) {
return new Promise(function(resolve, reject) {
if ( !/testsuite[_.]/.test(oTestPageConfig.fullpage) ) {
resolve(oTestPageConfig);
return;
}

if ( progressCallback ) {
progressCallback(oTestPageConfig.fullpage);
}

// check for an existing test page and check for test suite or page
getFile(oTestPageConfig.fullpage).then(function(sData) {
if (/(?:window\.suite\s*=|function\s*suite\s*\(\s*\)\s*{)/.test(sData)
|| (/data-sap-ui-testsuite/.test(sData)
&& !/sap\/ui\/test\/starter\/run-test/.test(sData)) ) {
// console.log("execute page ", sTestPage);

const frame = document.createElement("iframe");

const onSuiteReady = function onSuiteReady(oIFrame) {
findTestPages(oIFrame).then(function(aTests) {
if (frame.parentNode) {
frame.parentNode.removeChild(frame);
}

const bPass = aTests.filter(function(test) {
return !test.suite;
}).length === aTests.length;

resolve(assign({
tests: aTests,
simple: bPass
}, oTestPageConfig));
}, function(oError) {
karma.log("error", ["failed to load page '" + oTestPageConfig.fullpage + "'"]);
if (frame.parentNode) {
frame.parentNode.removeChild(frame);
}
resolve(assign({error: oError}, oTestPageConfig));
});
};

frame.style.display = "none";
frame.onload = function() {
if (typeof this.contentWindow.suite === "function") {
onSuiteReady(this);
} else {
// Wait for a CustomEvent in case window.suite isn't defined, yet
this.contentWindow.addEventListener("sap-ui-testsuite-ready", function() {
onSuiteReady(this);
}.bind(this));
}
};

const url = oTestPageConfig.fullpage;

// TODO: Add polyfill
// url.searchParams.set("sap-ui-xx-noless","true");
frame.src = url;
document.body.appendChild(frame);
} else {
resolve(oTestPageConfig);
}
});
}).catch(function(e) {
const text = e.message;
karma.log("error", ["Failed to load page '" + oTestPageConfig.fullpage + "': " + text]);
// resolve(assign({error: text}, oTestPageConfig));
return;
});
}

function sequence(aPages) {
return aPages.reduce( function(lastPromise, pageConfig) {
return lastPromise.then( function(lastResult) {
return checkTestPage(pageConfig).then( function(pageResult) {
lastResult.push(pageResult);
return lastResult;
});
});
}, Promise.resolve([])).then( function(a) {
return a;
});
}

/* function parallel(aPages) {
return Promise.all( aPages.map( (page) => checkTestPage(page) ) );
} */

function decorateWithTestsuite(aPages, sTestsuite) {
aPages.forEach( function(test) {
if ( test.testsuite === undefined ) {
test.testsuite = sTestsuite;
}
});
return aPages;
}

function findTestPages(oIFrame) {
return Promise.resolve(oIFrame.contentWindow.suite()).
then( function(oSuite) {
return (oSuite && oSuite.getTestPages() || []);
}).
then( function(aPages) {
return (decorateWithTestsuite(aPages, oIFrame.src));
}).
then( function(aPages) {
return sequence(aPages);
}).
catch( function() {
return [];
});
}

const origTestSuite = window.jsUnitTestSuite;
window.jsUnitTestSuite = TestSuite;
return checkTestPage({fullpage: sEntryPage}).finally(function() {
window.jsUnitTestSuite = origTestSuite;
});
}

window.findTestsuites = function(sEntryPage, progressCallback) {
return findPages(sEntryPage, progressCallback).then( function(result) {
const allSuites = [];
function collect(test) {
if ( Array.isArray(test.tests) ) {
test.tests.forEach(collect);
if ( test.simple ) {
allSuites.push(test.fullpage);
}
}
}
collect(result);
return allSuites;
});
};

window.findTests = function(entryPage, progressCallback) {
return findPages(entryPage, progressCallback).then( function(result) {
const allTests = [];
function collect(test) {
if ( Array.isArray(test.tests) ) {
test.tests.forEach(collect);
} else {
allTests.push(test);
}
}
collect(result);
return allTests;
});
};
})(window);
6 changes: 0 additions & 6 deletions lib/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ class Framework {
this.createProjectFilesPattern(config.basePath + `/{${webappFolder}/**,${webappFolder}/**/.*}`)
);
// No proxy required here, local files will be loaded via karma first

// Configure testrunner URL
this.config.client.ui5.testrunner = `/base/${webappFolder}/test-resources/sap/ui/qunit/testrunner.html`;
} else if (config.ui5.type === "library") {
this.config.files.push(
// Match all files (including dotfiles)
Expand All @@ -156,9 +153,6 @@ class Framework {
this.config.proxies[`/base/${this.replaceLast(srcFolder, "test-resources")}/`] = `/base/${testFolder}/`;
this.config.proxies[`/base/${this.replaceLast(testFolder, "resources")}/`] = `/base/${srcFolder}/`;
this.config.proxies[`/base/${this.replaceLast(testFolder, "test-resources")}/`] = `/base/${testFolder}/`;

// Configure testrunner URL
this.config.client.ui5.testrunner = `/base/${testFolder}/sap/ui/qunit/testrunner.html`;
}

// this.addPreprocessor();
Expand Down
Loading

0 comments on commit c2adeed

Please sign in to comment.