ethers.js/packages/tests/lib/reporter.js

149 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-07-13 15:03:56 +03:00
/* istanbul ignore file */
2019-09-01 07:13:35 +03:00
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-08 02:24:04 +03:00
exports.Reporter = exports.setLogFunc = void 0;
// Maximum time in seconds to suppress output
var MAX_DELAY = 30;
2019-09-01 07:13:35 +03:00
function getTime() {
return (new Date()).getTime();
}
function trunc(value) {
if (value >= 0) {
return Math.floor(value);
}
return Math.ceil(value);
}
function getDelta(t0) {
var ds = trunc((getTime() - t0) / 1000);
var minutes = trunc(ds / 60);
var seconds = String(trunc(ds) % 60);
while (seconds.length < 2) {
seconds = '0' + seconds;
}
return '(' + minutes + ':' + seconds + ')';
}
2020-08-28 05:04:58 +03:00
var _logFunc = console.log.bind(console);
function setLogFunc(logFunc) {
_logFunc = logFunc;
}
exports.setLogFunc = setLogFunc;
2019-09-01 07:13:35 +03:00
function Reporter(runner) {
var suites = [];
2020-05-05 06:01:04 +03:00
// Force Output; Keeps the console output alive with periodic updates
2019-09-01 07:13:35 +03:00
var lastOutput = getTime();
2020-05-05 06:01:04 +03:00
function forceOutput() {
if (((getTime() - lastOutput) / 1000) > MAX_DELAY) {
var currentSuite = suites[suites.length - 1];
log("[ Still running suite - test # " + (currentSuite ? currentSuite._countTotal : "0") + " ]");
}
}
var timer = setInterval(forceOutput, 1000);
2019-09-01 07:13:35 +03:00
function getIndent() {
var result = '';
for (var i = 0; i < suites.length; i++) {
result += ' ';
}
return result;
}
function log(message) {
if (!message) {
message = '';
}
2020-08-28 05:04:58 +03:00
_logFunc(getIndent() + message);
2019-09-01 07:13:35 +03:00
lastOutput = getTime();
}
runner.on('suite', function (suite) {
if (!suite.title) {
2020-01-07 03:00:55 +03:00
log('Testing: ' + (suite.suites ? 'Found ' + suite.suites.length + ' test suites' : ''));
2019-09-01 07:13:35 +03:00
}
else {
var filename = (suite.file || '').split('/').pop();
if (filename) {
filename = ' (' + filename + ')';
}
log('Test Suite: ' + suite.title + filename);
}
suites.push(suite);
suite._t0 = getTime();
suite._countFail = 0;
suite._countPass = 0;
2020-06-01 12:04:12 +03:00
suite._countSkip = 0;
2019-09-01 07:13:35 +03:00
suite._countTotal = 0;
});
runner.on('suite end', function () {
var suite = suites.pop();
2020-06-01 12:04:12 +03:00
var extras = [];
if (suite._countSkip) {
extras.push(suite._countSkip + " skipped");
}
2019-09-01 07:13:35 +03:00
if (suite._countTotal > suite._countPass) {
2020-06-01 12:04:12 +03:00
extras.push((suite._countTotal - suite._countPass) + " failed");
}
var extra = "";
if (extras.length) {
extra = " (" + extras.join(",") + ") ******** WARNING! ********";
2019-09-01 07:13:35 +03:00
}
2020-08-28 05:04:58 +03:00
log(" Total Tests: " + suite._countPass + "/" + suite._countTotal + " passed " + getDelta(suite._t0) + " " + extra + " \n");
2019-09-01 07:13:35 +03:00
if (suites.length > 0) {
var currentSuite = suites[suites.length - 1];
currentSuite._countFail += suite._countFail;
currentSuite._countPass += suite._countPass;
2020-06-01 12:04:12 +03:00
currentSuite._countSkip += suite._countSkip;
2019-09-01 07:13:35 +03:00
currentSuite._countTotal += suite._countTotal;
}
2020-05-05 06:01:04 +03:00
else {
clearTimeout(timer);
2020-09-04 10:21:51 +03:00
var status_1 = (suite._countPass === suite._countTotal) ? 0 : 1;
log("# status:" + status_1);
// Force quit after 5s
setTimeout(function () {
process.exit(status_1);
}, 5000);
2019-09-01 07:13:35 +03:00
}
2020-05-05 06:01:04 +03:00
});
runner.on('test', function (test) {
forceOutput();
2020-09-04 10:21:51 +03:00
if (test._currentRetry === 0) {
var currentSuite = suites[suites.length - 1];
currentSuite._countTotal++;
}
2019-09-01 07:13:35 +03:00
});
runner.on('fail', function (test, error) {
var currentSuite = suites[suites.length - 1];
currentSuite._countFail++;
var 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);
error.toString().split('\n').forEach(function (line) {
log(' ' + line);
});
Object.keys(error).forEach(function (key) {
log(' ' + key + ': ' + error[key]);
});
if (error.stack) {
log("Stack Trace:");
error.stack.split('\n').forEach(function (line) {
log(' ' + line);
});
}
});
runner.on('pass', function (test) {
var currentSuite = suites[suites.length - 1];
currentSuite._countPass++;
});
2020-06-01 12:04:12 +03:00
runner.on('pending', function (test) {
var currentSuite = suites[suites.length - 1];
currentSuite._countSkip++;
});
2019-09-01 07:13:35 +03:00
}
exports.Reporter = Reporter;
2020-07-13 15:03:56 +03:00
//# sourceMappingURL=reporter.js.map