2019-01-04 11:29:27 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to convert a status to an icon.
|
|
|
|
*
|
|
|
|
* @param {string} status
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2019-08-20 18:13:05 +02:00
|
|
|
function statusToIcon(status) {
|
|
|
|
return {
|
2019-01-04 11:29:27 +01:00
|
|
|
erroring: "🔥",
|
|
|
|
failing: "❌",
|
|
|
|
passing: "✔️️",
|
2019-08-20 18:13:05 +02:00
|
|
|
}[status] || "?";
|
|
|
|
}
|
2019-01-04 11:29:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Counts test statuses.
|
|
|
|
*
|
2019-08-20 18:13:05 +02:00
|
|
|
* @param {Object} testStatus
|
2019-01-04 11:29:27 +01:00
|
|
|
* @param {Object} testResult
|
|
|
|
*/
|
|
|
|
function handleTestResult(testStatus, testResult) {
|
|
|
|
testStatus.allTestsPassing = testStatus.allTestsPassing && testResult.status === "passing";
|
2019-08-20 18:13:05 +02:00
|
|
|
testStatus.counts[testResult.status] = (testStatus.counts[testResult.status] || 0) + 1;
|
2019-01-04 11:29:27 +01:00
|
|
|
testStatus.counts.total += 1;
|
2020-03-13 15:59:48 +01:00
|
|
|
|
|
|
|
if (testResult.duration > 2000) {
|
2020-03-13 16:06:40 +01:00
|
|
|
console.log(`'${testResult.test.name}' took ${(testResult.duration / 1000).toFixed(1)}s to complete`);
|
2020-03-13 15:59:48 +01:00
|
|
|
}
|
2019-01-04 11:29:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-20 18:13:05 +02:00
|
|
|
* Log each test result, count tests and failures.
|
2019-01-04 17:31:06 +01:00
|
|
|
*
|
2019-01-04 11:29:27 +01:00
|
|
|
* @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));
|
|
|
|
|
2019-08-20 18:13:05 +02:00
|
|
|
console.log();
|
2019-01-04 11:29:27 +01:00
|
|
|
for (const testStatusCount in testStatus.counts) {
|
|
|
|
const count = testStatus.counts[testStatusCount];
|
|
|
|
if (count > 0) {
|
2019-08-20 18:13:05 +02:00
|
|
|
console.log(testStatusCount.toUpperCase() + "\t" + count);
|
2019-01-04 11:29:27 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-20 18:13:05 +02:00
|
|
|
console.log();
|
|
|
|
|
|
|
|
// Print error messages for tests that didn't pass
|
|
|
|
results.filter(res => res.status !== "passing").forEach(testResult => {
|
|
|
|
console.log([
|
|
|
|
statusToIcon(testResult.status),
|
|
|
|
testResult.test.name
|
|
|
|
].join(" "));
|
|
|
|
|
|
|
|
if (testResult.output) {
|
|
|
|
console.log(
|
|
|
|
testResult.output
|
|
|
|
.trim()
|
|
|
|
.replace(/^/, "\t")
|
|
|
|
.replace(/\n/g, "\n\t")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log();
|
2019-01-04 11:29:27 +01:00
|
|
|
|
|
|
|
process.exit(testStatus.allTestsPassing ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fail if the process takes longer than 60 seconds.
|
|
|
|
*/
|
|
|
|
export function setLongTestFailure() {
|
2020-03-13 16:06:40 +01:00
|
|
|
const timeLimit = 120;
|
2019-01-04 11:29:27 +01:00
|
|
|
setTimeout(function() {
|
2020-03-13 15:59:48 +01:00
|
|
|
console.log(`Tests took longer than ${timeLimit} seconds to run, returning.`);
|
2019-01-04 11:29:27 +01:00
|
|
|
process.exit(1);
|
2020-03-13 15:59:48 +01:00
|
|
|
}, timeLimit * 1000);
|
2019-01-04 11:29:27 +01:00
|
|
|
}
|