From 35afe56b5cfb31ed0fadccc2a5b79f671ff1ab30 Mon Sep 17 00:00:00 2001 From: Pablo Grass Date: Thu, 20 Dec 2018 11:59:19 +0100 Subject: [PATCH] tests: throw in case we encounter unhandled promise rejection As opposed to just showing a warning that might get eaten in cli when multple jest child processes work in parallel. See https://github.com/facebook/jest/issues/3251 This is a bit unsatifactory as it makes the parent jest process hang in case there are such occurances. Bug: T212409 --- tests/config/setup.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/config/setup.ts b/tests/config/setup.ts index 159f16b..04141ff 100644 --- a/tests/config/setup.ts +++ b/tests/config/setup.ts @@ -1,3 +1,15 @@ beforeEach( () => { expect.hasAssertions(); } ); + +// Break on unhandled promise rejection (default warning might be overlooked) +// https://github.com/facebook/jest/issues/3251#issuecomment-299183885 +// However... +// https://stackoverflow.com/questions/51957531/jest-how-to-throw-an-error-inside-a-unhandledrejection-handler +if ( typeof process.env.LISTENING_TO_UNHANDLED_REJECTION === 'undefined' ) { + process.on( 'unhandledRejection', ( unhandledRejectionWarning ) => { + throw unhandledRejectionWarning; // see stack trace for test at fault + } ); + // Avoid memory leak by adding too many listeners + process.env.LISTENING_TO_UNHANDLED_REJECTION = 'yes'; +}