2020-07-13 13:48:33 +03:00
|
|
|
/* istanbul ignore file */
|
2019-05-15 01:25:46 +03:00
|
|
|
'use strict';
|
|
|
|
|
2020-02-13 23:27:56 +03:00
|
|
|
// Maximum time in seconds to suppress output
|
|
|
|
const MAX_DELAY = 30;
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
function getTime(): number {
|
|
|
|
return (new Date()).getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
function trunc(value: number): number {
|
|
|
|
if (value >= 0) { return Math.floor(value); }
|
|
|
|
return Math.ceil(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDelta(t0: number): string {
|
|
|
|
let ds = trunc((getTime() - t0) / 1000);
|
|
|
|
let minutes = trunc(ds / 60);
|
|
|
|
let seconds = String(trunc(ds) % 60);
|
|
|
|
while (seconds.length < 2) { seconds = '0' + seconds; }
|
|
|
|
return '(' + minutes + ':' + seconds + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Suite {
|
|
|
|
title: string;
|
|
|
|
file: string;
|
|
|
|
suites: Array<Suite>;
|
|
|
|
_t0: number;
|
|
|
|
_countFail: number;
|
|
|
|
_countPass: number;
|
|
|
|
_countTotal: number;
|
2020-06-01 11:47:06 +03:00
|
|
|
_countSkip: number;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Runner {
|
|
|
|
on(event: string, callback: (...args: Array<any>) => void): Runner;
|
|
|
|
}
|
|
|
|
|
2020-08-28 04:58:34 +03:00
|
|
|
export type LogFunc = (message: string) => void;
|
|
|
|
|
|
|
|
let _logFunc: LogFunc = console.log.bind(console);
|
|
|
|
export function setLogFunc(logFunc: LogFunc) {
|
|
|
|
_logFunc = logFunc
|
|
|
|
}
|
|
|
|
|
2019-09-07 00:48:26 +03:00
|
|
|
export function Reporter(runner: Runner) {
|
2020-08-28 04:58:34 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
let suites: Array<Suite> = [];
|
|
|
|
|
2020-05-05 05:47:06 +03:00
|
|
|
// Force Output; Keeps the console output alive with periodic updates
|
2019-05-15 01:25:46 +03:00
|
|
|
let lastOutput = getTime();
|
2020-05-05 05:47:06 +03:00
|
|
|
function forceOutput() {
|
|
|
|
if (((getTime() - lastOutput) / 1000) > MAX_DELAY) {
|
|
|
|
const currentSuite = suites[suites.length - 1];
|
|
|
|
log(`[ Still running suite - test # ${ (currentSuite ? currentSuite._countTotal: "0") } ]`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const timer = setInterval(forceOutput, 1000);
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
function getIndent(): string {
|
|
|
|
let result = '';
|
|
|
|
for (let i = 0; i < suites.length; i++) { result += ' '; }
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function log(message?: string): void {
|
|
|
|
if (!message) { message = ''; }
|
2020-08-28 04:58:34 +03:00
|
|
|
_logFunc(getIndent() + message);
|
2019-05-15 01:25:46 +03:00
|
|
|
lastOutput = getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.on('suite', function(suite: Suite) {
|
|
|
|
if (!suite.title) {
|
2019-08-19 18:53:58 +03:00
|
|
|
log('Testing: ' + (suite.suites ? 'Found ' + suite.suites.length + ' test suites' : ''));
|
2019-05-15 01:25:46 +03:00
|
|
|
} else {
|
|
|
|
let filename = (suite.file || '').split('/').pop();
|
|
|
|
if (filename) { filename = ' (' + filename + ')'; }
|
|
|
|
log('Test Suite: ' + suite.title + filename);
|
|
|
|
}
|
2020-06-01 11:47:06 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
suites.push(suite);
|
2020-06-01 11:47:06 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
suite._t0 = getTime();
|
|
|
|
suite._countFail = 0;
|
|
|
|
suite._countPass = 0;
|
2020-06-01 11:47:06 +03:00
|
|
|
suite._countSkip = 0;
|
2019-05-15 01:25:46 +03:00
|
|
|
suite._countTotal = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('suite end', function() {
|
|
|
|
let suite = suites.pop();
|
2020-06-01 11:47:06 +03:00
|
|
|
|
|
|
|
let extras = [ ];
|
|
|
|
|
|
|
|
if (suite._countSkip) {
|
|
|
|
extras.push(suite._countSkip + " skipped");
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
if (suite._countTotal > suite._countPass) {
|
2020-06-01 11:47:06 +03:00
|
|
|
extras.push((suite._countTotal - suite._countPass) + " failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
let extra = "";
|
|
|
|
if (extras.length) {
|
|
|
|
extra = " (" + extras.join(",") + ") ******** WARNING! ********";
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
2020-06-01 11:47:06 +03:00
|
|
|
|
2020-08-28 04:58:34 +03:00
|
|
|
log(` Total Tests: ${ suite._countPass }/${ suite._countTotal } passed ${ getDelta(suite._t0) } ${ extra} \n`);
|
2020-05-05 05:47:06 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
if (suites.length > 0) {
|
|
|
|
let currentSuite = suites[suites.length - 1];
|
|
|
|
currentSuite._countFail += suite._countFail;
|
|
|
|
currentSuite._countPass += suite._countPass;
|
2020-06-01 11:47:06 +03:00
|
|
|
currentSuite._countSkip += suite._countSkip;
|
2019-05-15 01:25:46 +03:00
|
|
|
currentSuite._countTotal += suite._countTotal;
|
2020-05-05 05:47:06 +03:00
|
|
|
} else {
|
|
|
|
clearTimeout(timer);
|
2020-09-04 10:15:07 +03:00
|
|
|
const status = (suite._countPass === suite._countTotal) ? 0: 1;
|
|
|
|
log(`# status:${ status }`);
|
|
|
|
|
|
|
|
// Force quit after 5s
|
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(status);
|
|
|
|
}, 5000);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-13 23:27:56 +03:00
|
|
|
runner.on('test', function(test) {
|
|
|
|
forceOutput();
|
2020-09-04 10:15:07 +03:00
|
|
|
if (test._currentRetry === 0) {
|
|
|
|
const currentSuite = suites[suites.length - 1];
|
|
|
|
currentSuite._countTotal++;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('fail', function(test, error) {
|
|
|
|
let currentSuite = suites[suites.length - 1];
|
|
|
|
currentSuite._countFail++;
|
|
|
|
|
|
|
|
let countFail = currentSuite._countFail;
|
|
|
|
|
|
|
|
if (countFail > 100) {
|
|
|
|
if (countFail === 101) {
|
|
|
|
log('[ Over 100 errors; skipping remaining suite output ]');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (countFail > 25) {
|
|
|
|
log('Failure: ' + test.title + ' (too many errors; skipping dump)');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log('Failure: ' + test.title);
|
2020-06-01 11:47:06 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
error.toString().split('\n').forEach((line: string) => {
|
|
|
|
log(' ' + line);
|
|
|
|
});
|
2020-06-01 11:47:06 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
Object.keys(error).forEach(function(key) {
|
|
|
|
log(' ' + key + ': ' + error[key]);
|
|
|
|
});
|
2020-06-01 11:47:06 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
if (error.stack) {
|
|
|
|
log("Stack Trace:");
|
|
|
|
error.stack.split('\n').forEach((line: string) => {
|
|
|
|
log(' ' + line);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
runner.on('pass', function(test) {
|
|
|
|
let currentSuite = suites[suites.length - 1];
|
|
|
|
currentSuite._countPass++;
|
|
|
|
});
|
2020-06-01 11:47:06 +03:00
|
|
|
|
|
|
|
runner.on('pending', function(test) {
|
|
|
|
let currentSuite = suites[suites.length - 1];
|
|
|
|
currentSuite._countSkip++;
|
|
|
|
});
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|