From 885fdd8f23cdb5f63167524c79fb31eaf8c49756 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 24 Aug 2017 16:07:44 +0200 Subject: [PATCH] improve based on CR --- .../jest-cli/src/reporters/coverage_worker.js | 6 ++++++ packages/jest-jasmine2/src/jasmine/Env.js | 20 +++++++++++++++++++ packages/jest-jasmine2/src/queue_runner.js | 6 +++--- packages/jest-runner/src/test_worker.js | 6 ++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/jest-cli/src/reporters/coverage_worker.js b/packages/jest-cli/src/reporters/coverage_worker.js index 02c647a57293..25ef877458ba 100644 --- a/packages/jest-cli/src/reporters/coverage_worker.js +++ b/packages/jest-cli/src/reporters/coverage_worker.js @@ -37,6 +37,12 @@ function formatCoverageError(error, filename: Path): SerializableError { }; } +// Make sure uncaught errors are logged before we exit. +process.on('uncaughtException', err => { + console.error(err.stack); + process.exit(1); +}); + module.exports = ( {config, globalConfig, path}: CoverageWorkerData, callback: WorkerCallback, diff --git a/packages/jest-jasmine2/src/jasmine/Env.js b/packages/jest-jasmine2/src/jasmine/Env.js index 79eba336bd7f..3b885618b8a6 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.js +++ b/packages/jest-jasmine2/src/jasmine/Env.js @@ -197,6 +197,17 @@ module.exports = function(j$) { } }; + // Need to ensure we are the only ones handling these exceptions. + const oldListenersException = process + .listeners('uncaughtException') + .slice(); + const oldListenersRejection = process + .listeners('unhandledRejection') + .slice(); + + process.removeAllListeners('uncaughtException'); + process.removeAllListeners('unhandledRejection'); + process.on('uncaughtException', uncaught); process.on('unhandledRejection', uncaught); @@ -229,6 +240,15 @@ module.exports = function(j$) { process.removeListener('uncaughtException', uncaught); process.removeListener('unhandledRejection', uncaught); + + // restore previous exception handlers + oldListenersException.forEach(listener => { + process.on('uncaughtException', listener); + }); + + oldListenersRejection.forEach(listener => { + process.on('unhandledRejection', listener); + }); }; this.addReporter = function(reporterToAdd) { diff --git a/packages/jest-jasmine2/src/queue_runner.js b/packages/jest-jasmine2/src/queue_runner.js index d354b8fbd194..148ec7ac673a 100644 --- a/packages/jest-jasmine2/src/queue_runner.js +++ b/packages/jest-jasmine2/src/queue_runner.js @@ -71,15 +71,15 @@ function queueRunner(options: Options) { ); }; - const res = options.queueableFns.reduce( + const result = options.queueableFns.reduce( (promise, fn) => promise.then(() => mapper(fn)), Promise.resolve(), ); return { cancel: token.cancel.bind(token), - catch: res.catch.bind(res), - then: res.then.bind(res), + catch: result.catch.bind(result), + then: result.then.bind(result), }; } diff --git a/packages/jest-runner/src/test_worker.js b/packages/jest-runner/src/test_worker.js index 430b0e60be62..79baeefad59d 100644 --- a/packages/jest-runner/src/test_worker.js +++ b/packages/jest-runner/src/test_worker.js @@ -12,6 +12,12 @@ import type {GlobalConfig, Path, ProjectConfig} from 'types/Config'; import type {SerializableError, TestResult} from 'types/TestResult'; import type {RawModuleMap} from 'types/HasteMap'; +// Make sure uncaught errors are logged before we exit. +process.on('uncaughtException', err => { + console.error(err.stack); + process.exit(1); +}); + import HasteMap from 'jest-haste-map'; import {separateMessageFromStack} from 'jest-message-util'; import Runtime from 'jest-runtime';