refactor TestRegister and some test suite functions into common area

This commit is contained in:
d98762625 2019-01-04 10:29:27 +00:00
parent a4de937eb6
commit f22e9ceec6
64 changed files with 211 additions and 175 deletions

126
tests/lib/TestRegister.mjs Normal file
View file

@ -0,0 +1,126 @@
/**
* TestRegister.js
*
* This is so individual files can register their tests in one place, and
* ensure that they will get run by the frontend.
*
* @author tlwr [toby@toby.codes]
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Chef from "../../src/core/Chef";
/**
* Object to store and run the list of tests.
*
* @class
* @constructor
*/
class TestRegister {
/**
* initialise with no tests
*/
constructor() {
this.tests = [];
this.apiTests = [];
}
/**
* Add a list of tests to the register.
*
* @param {Object[]} tests
*/
addTests(tests) {
this.tests = this.tests.concat(tests);
}
/**
* Add a list of api tests to the register
* @param {Object[]} tests
*/
addApiTests(tests) {
this.apiTests = this.apiTests.concat(tests);
}
/**
* Runs all the tests in the register.
*/
runTests () {
console.log("run tests");
return Promise.all(
this.tests.map(function(test, i) {
const chef = new Chef();
return chef.bake(
test.input,
test.recipeConfig,
{},
0,
false
).then(function(result) {
const ret = {
test: test,
status: null,
output: null,
};
if (result.error) {
if (test.expectedError) {
ret.status = "passing";
} else {
ret.status = "erroring";
ret.output = result.error.displayStr;
}
} else {
if (test.expectedError) {
ret.status = "failing";
ret.output = "Expected an error but did not receive one.";
} else if (result.result === test.expectedOutput) {
ret.status = "passing";
} else if (test.hasOwnProperty("expectedMatch") && test.expectedMatch.test(result.result)) {
ret.status = "passing";
} else {
ret.status = "failing";
const expected = test.expectedOutput ? test.expectedOutput :
test.expectedMatch ? test.expectedMatch.toString() : "unknown";
ret.output = [
"Expected",
"\t" + expected.replace(/\n/g, "\n\t"),
"Received",
"\t" + result.result.replace(/\n/g, "\n\t"),
].join("\n");
}
}
return ret;
});
})
);
}
/**
* Run all api related tests and wrap results in report format
*/
runApiTests() {
return Promise.all(this.apiTests.map(async function(test, i) {
const result = {
test: test,
status: null,
output: null,
};
try {
await test.run();
result.status = "passing";
} catch (e) {
result.status = "erroring";
result.output = e.message;
}
return result;
}));
}
}
// Export an instance to make a singleton
export default new TestRegister();

102
tests/lib/utils.mjs Normal file
View file

@ -0,0 +1,102 @@
/**
* Utils for test suite
*
* @author d98762625@gmail.com
* @author tlwr [toby@toby.codes]
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
// Define global environment functions
global.ENVIRONMENT_IS_WORKER = function() {
return typeof importScripts === "function";
};
global.ENVIRONMENT_IS_NODE = function() {
return typeof process === "object" && typeof require === "function";
};
global.ENVIRONMENT_IS_WEB = function() {
return typeof window === "object";
};
/**
* Helper function to convert a status to an icon.
*
* @param {string} status
* @returns {string}
*/
const statusToIcon = function statusToIcon(status) {
const icons = {
erroring: "🔥",
failing: "❌",
passing: "✔️️",
};
return icons[status] || "?";
};
/**
* Displays a given test result in the console.
* Counts test statuses.
*
* @param {Object} testStatusCounts
* @param {Object} testResult
*/
function handleTestResult(testStatus, testResult) {
testStatus.allTestsPassing = testStatus.allTestsPassing && testResult.status === "passing";
const newCount = (testStatus.counts[testResult.status] || 0) + 1;
testStatus.counts[testResult.status] = newCount;
testStatus.counts.total += 1;
console.log([
statusToIcon(testResult.status),
testResult.test.name
].join(" "));
if (testResult.output) {
console.log(
testResult.output
.trim()
.replace(/^/, "\t")
.replace(/\n/g, "\n\t")
);
}
}
/**
* Log each test result, count tests and failures. Log test suite run duration.
*
* @param {Object} testStatus - object describing test run data
* @param {Object[]} results - results from TestRegister
*/
export function logTestReport(testStatus, results) {
results.forEach(r => handleTestResult(testStatus, r));
console.log("\n");
for (const testStatusCount in testStatus.counts) {
const count = testStatus.counts[testStatusCount];
if (count > 0) {
console.log(testStatusCount.toUpperCase(), count);
}
}
if (!testStatus.allTestsPassing) {
console.log("\nFailing tests:\n");
results.filter(r => r.status !== "passing").forEach(handleTestResult);
}
console.log(`Tests took ${(testStatus.finish - testStatus.start) / 1000} seconds`);
process.exit(testStatus.allTestsPassing ? 0 : 1);
}
/**
* Fail if the process takes longer than 60 seconds.
*/
export function setLongTestFailure() {
setTimeout(function() {
console.log("Tests took longer than 60 seconds to run, returning.");
process.exit(1);
}, 60 * 1000);
}