From c304d37cbd363acd388893b6e1f51e279732768a Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Mon, 13 Jul 2020 07:23:41 -0400 Subject: [PATCH] Added coverage keep-alive reporter. --- packages/tests/src.ts/reporter-keepalive.ts | 93 +++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 packages/tests/src.ts/reporter-keepalive.ts diff --git a/packages/tests/src.ts/reporter-keepalive.ts b/packages/tests/src.ts/reporter-keepalive.ts new file mode 100644 index 000000000..a06a28980 --- /dev/null +++ b/packages/tests/src.ts/reporter-keepalive.ts @@ -0,0 +1,93 @@ +/* istanbul ignore file */ + +'use strict'; + +// Maximum time in seconds to suppress output +const MAX_DELAY = 30; + +function getTime(): number { + return (new Date()).getTime(); +} + +interface Suite { + title: string; + file: string; + suites: Array; +} + +interface Runner { + on(event: string, callback: (...args: Array) => void): Runner; +} + +export function ReporterKeepAlive(runner: Runner) { + let suites = 0; + let fails = 0; + const errors: Array = [ ]; + + const stdoutWrite = process.stdout.write.bind(process.stdout); + //process.stdout.write = function(buffer: string | Uint8Array, cb?: (err?: Error) => void): boolean { + process.stdout.write = function(...args: Array): boolean { + return stdoutWrite("*"); + } + + // Force Output; Keeps the console output alive with periodic updates + let lastOutput = getTime(); + function forceOutput() { + if (((getTime() - lastOutput) / 1000) > MAX_DELAY) { + log("."); + } + } + const timer = setInterval(forceOutput, 1000); + + function log(message: string): void { + stdoutWrite(message); + lastOutput = getTime(); + } + + runner.on('suite', function(suite: Suite) { + suites++; + fails = 0; + log("["); + }); + + runner.on('suite end', function() { + suites--; + log("]"); + + if (suites === 0) { + process.stdout.write = stdoutWrite; + clearTimeout(timer); + console.log("") + if (errors.length) { + console.log("---------------") + errors.forEach((error, index) => { + if (index > 0) { console.log(""); } + console.log(error); + }); + } + console.log("---------------") + } + }); + + runner.on('test', function(test) { + forceOutput(); + }); + + runner.on('fail', function(test, error) { + fails++; + if (fails < 10) { + errors.push(`Error #${ errors.length } (${ test.title }): ${ error.message }\n${ error.stack }`); + log("!"); + } + }); + + runner.on('pass', function(test) { + forceOutput(); + + }); + + runner.on('pending', function(test) { + log("?"); + }); +} +