78 lines
9.1 KiB
JavaScript
78 lines
9.1 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.stopHandlingTestErrors = exports.startHandlingTestErrors = exports.removeRunningTest = exports.addRunningTest = exports.registerErrorHandlers = exports.handleUnexpectedError = exports.formatError = void 0;
|
||
|
const test_run_1 = require("../errors/test-run");
|
||
|
const util_1 = __importDefault(require("util"));
|
||
|
const runningTests = {};
|
||
|
let handlingTestErrors = false;
|
||
|
function printErrorMessagesAndTerminate(...messages) {
|
||
|
// eslint-disable-next-line no-console
|
||
|
messages.map(msg => console.log(msg));
|
||
|
// eslint-disable-next-line no-process-exit
|
||
|
setTimeout(() => process.exit(1), 0);
|
||
|
}
|
||
|
function handleTestRunError(ErrorCtor, message) {
|
||
|
Object.values(runningTests).forEach(testRun => {
|
||
|
testRun.addError(new ErrorCtor(message));
|
||
|
removeRunningTest(testRun);
|
||
|
});
|
||
|
}
|
||
|
function handleError(ErrorCtor, message) {
|
||
|
if (handlingTestErrors)
|
||
|
handleTestRunError(ErrorCtor, message);
|
||
|
else
|
||
|
printErrorMessagesAndTerminate(message);
|
||
|
}
|
||
|
function formatUnhandledRejectionReason(reason) {
|
||
|
const reasonType = typeof reason;
|
||
|
const isPrimitiveType = reasonType !== 'object' && reasonType !== 'function';
|
||
|
if (isPrimitiveType)
|
||
|
return String(reason);
|
||
|
if (reason instanceof Error)
|
||
|
return reason.stack;
|
||
|
return util_1.default.inspect(reason, { depth: 2, breakLength: Infinity });
|
||
|
}
|
||
|
function formatError(ErrorCtor, error) {
|
||
|
if (ErrorCtor === test_run_1.UncaughtExceptionError)
|
||
|
return error.stack;
|
||
|
if (ErrorCtor === test_run_1.UnhandledPromiseRejectionError)
|
||
|
return formatUnhandledRejectionReason(error);
|
||
|
return error;
|
||
|
}
|
||
|
exports.formatError = formatError;
|
||
|
function handleUnexpectedError(ErrorCtor, error) {
|
||
|
try {
|
||
|
const formattedError = typeof error === 'string' ? error : formatError(ErrorCtor, error);
|
||
|
handleError(ErrorCtor, formattedError);
|
||
|
}
|
||
|
catch (e) {
|
||
|
printErrorMessagesAndTerminate(error, e);
|
||
|
}
|
||
|
}
|
||
|
exports.handleUnexpectedError = handleUnexpectedError;
|
||
|
function registerErrorHandlers() {
|
||
|
process.on('unhandledRejection', e => handleUnexpectedError(test_run_1.UnhandledPromiseRejectionError, e));
|
||
|
process.on('uncaughtException', e => handleUnexpectedError(test_run_1.UncaughtExceptionError, e));
|
||
|
}
|
||
|
exports.registerErrorHandlers = registerErrorHandlers;
|
||
|
function addRunningTest(testRun) {
|
||
|
runningTests[testRun.id] = testRun;
|
||
|
}
|
||
|
exports.addRunningTest = addRunningTest;
|
||
|
function removeRunningTest(testRun) {
|
||
|
if (testRun)
|
||
|
delete runningTests[testRun.id];
|
||
|
}
|
||
|
exports.removeRunningTest = removeRunningTest;
|
||
|
function startHandlingTestErrors() {
|
||
|
handlingTestErrors = true;
|
||
|
}
|
||
|
exports.startHandlingTestErrors = startHandlingTestErrors;
|
||
|
function stopHandlingTestErrors() {
|
||
|
handlingTestErrors = false;
|
||
|
}
|
||
|
exports.stopHandlingTestErrors = stopHandlingTestErrors;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaGFuZGxlLWVycm9ycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlscy9oYW5kbGUtZXJyb3JzLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLGlEQUE0RjtBQUM1RixnREFBd0I7QUFFeEIsTUFBTSxZQUFZLEdBQU8sRUFBRSxDQUFDO0FBQzVCLElBQUksa0JBQWtCLEdBQUcsS0FBSyxDQUFDO0FBRS9CLFNBQVMsOEJBQThCLENBQUU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