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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaGFuZGxlLWVycm9ycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlscy9oYW5kbGUtZXJyb3JzLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLGlEQUE0RjtBQUM1RixnREFBd0I7QUFFeEIsTUFBTSxZQUFZLEdBQU8sRUFBRSxDQUFDO0FBQzVCLElBQUksa0JBQWtCLEdBQUcsS0FBSyxDQUFDO0FBRS9CLFNBQVMsOEJBQThCLENBQUUsR0FBRyxRQUFRO0lBQ2hELHNDQUFzQztJQUN0QyxRQUFRLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxFQUFFLENBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO0lBRXRDLDJD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