UNPKG

dockest

Version:

Dockest is an integration testing tool aimed at alleviating the process of evaluating unit tests whilst running multi-container Docker applications.

80 lines 3.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupExitHandler = void 0; const fs_1 = __importDefault(require("fs")); const errors_1 = require("../../errors"); const logger_1 = require("../../logger"); const teardown_single_1 = require("../../utils/teardown-single"); const LOG_PREFIX = '[Exit Handler]'; const setupExitHandler = ({ dumpErrors, exitHandler: customExitHandler, mutables, mutables: { runners }, perfStart, }) => { let exitInProgress = false; const exitHandler = async (errorPayload) => { if (exitInProgress) { return; } // Ensure the exit handler is only invoced once exitInProgress = true; if (mutables.jestRanWithResult) { return; } if (errorPayload.reason instanceof errors_1.BaseError) { const { payload: { error, runner, ...restPayload }, message, name, stack, } = errorPayload.reason; const logPayload = { data: { name, stack, }, }; runner && (logPayload.data.serviceName = runner.serviceName); runner && runner.containerId && (logPayload.data.containerId = runner.containerId); error && (logPayload.data.error = error); restPayload && typeof restPayload === 'object' && Object.keys(restPayload).length > 0 && (logPayload.data.restPayload = restPayload); logger_1.Logger.error(`${LOG_PREFIX} ${message}`, logPayload); } else { logger_1.Logger.error(`${LOG_PREFIX} ${JSON.stringify(errorPayload, null, 2)}`); } if (customExitHandler && typeof customExitHandler === 'function') { await customExitHandler(errorPayload); } for (const runner of Object.values(runners)) { await (0, teardown_single_1.teardownSingle)({ runner }); } if (dumpErrors === true) { const dumpPath = `${process.cwd()}/dockest-error.json`; const dumpPayload = { errorPayload, timestamp: new Date(), }; try { fs_1.default.writeFileSync(dumpPath, JSON.stringify(dumpPayload, null, 2)); } catch (dumpError) { logger_1.Logger.debug(`Failed to dump error to ${dumpPath}`, { data: { dumpError, dumpPayload } }); } } logger_1.Logger.measurePerformance(perfStart, { logPrefix: LOG_PREFIX }); process.exit(errorPayload.code || 1); }; // keeps the program from closing instantly process.stdin.resume(); // FIXME: causes "Jest has detected the following 1 open handle potentially keeping Jest from exiting:" // do something when app is closing process.on('exit', async (code) => exitHandler({ trap: 'exit', code })); // catches ctrl+c event process.on('SIGINT', async (signal) => exitHandler({ trap: 'SIGINT', signal })); // catches "kill pid" (for example: nodemon restart) process.on('SIGUSR1', async () => exitHandler({ trap: 'SIGUSR1' })); process.on('SIGUSR2', async () => exitHandler({ trap: 'SIGUSR2' })); // catches uncaught exceptions process.on('uncaughtException', async (error) => exitHandler({ trap: 'uncaughtException', error })); // catches unhandled promise rejections process.on('unhandledRejection', async (reason, promise) => exitHandler({ trap: 'unhandledRejection', reason, promise })); }; exports.setupExitHandler = setupExitHandler; //# sourceMappingURL=setup-exit-handler.js.map