From 0840a789d21f20b03920b0474eb645208580386d Mon Sep 17 00:00:00 2001 From: Dmitri Gabbasov Date: Fri, 22 Oct 2021 20:37:52 +0300 Subject: [PATCH 1/2] fix: avoid uncaught promise rejections when concurrent tests fail --- CHANGELOG.md | 2 ++ e2e/__tests__/jasmineAsync.test.ts | 9 +++++++++ .../concurrent-parallel-failure.test.js | 16 ++++++++++++++++ .../legacy-code-todo-rewrite/jestAdapterInit.ts | 3 +++ .../jest-jasmine2/src/jasmineAsyncInstall.ts | 3 +++ 5 files changed, 33 insertions(+) create mode 100644 e2e/jasmine-async/__tests__/concurrent-parallel-failure.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index c37f53b3699e..21c34c5a6748 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-circus, jest-jasmine2]` Avoid false concurrent test failures due to unhandled promise rejections + ### Chore & Maintenance ### Performance diff --git a/e2e/__tests__/jasmineAsync.test.ts b/e2e/__tests__/jasmineAsync.test.ts index 3006a04b3810..8b6cfb93ec1c 100644 --- a/e2e/__tests__/jasmineAsync.test.ts +++ b/e2e/__tests__/jasmineAsync.test.ts @@ -168,4 +168,13 @@ describe('async jasmine', () => { expect(result.exitCode).toBe(0); }); + + it('works when another test fails while one is running', () => { + const {json} = runWithJson('jasmine-async', [ + 'concurrent-parallel-failure.test.js', + ]); + expect(json.numTotalTests).toBe(2); + expect(json.numPassedTests).toBe(1); + expect(json.numFailedTests).toBe(1); + }); }); diff --git a/e2e/jasmine-async/__tests__/concurrent-parallel-failure.test.js b/e2e/jasmine-async/__tests__/concurrent-parallel-failure.test.js new file mode 100644 index 000000000000..e4869be91465 --- /dev/null +++ b/e2e/jasmine-async/__tests__/concurrent-parallel-failure.test.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +it.concurrent('Good Test', async () => { + await new Promise(r => setTimeout(r, 100)); +}); + +it.concurrent('Bad Test', async () => { + expect('a').toBe('b'); +}); diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index d7487477e31d..43bf59f1af4e 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -93,6 +93,9 @@ export const initialize = async ({ // that will result in this test to be skipped, so we'll be executing the promise function anyway, // even if it ends up being skipped. const promise = mutex(() => testFn()); + // Avoid triggering the uncaught promise rejection handler in case the test errors before + // being awaited on. + promise.catch(() => {}); globalsObject.test(testName, () => promise, timeout); }; diff --git a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts index 6a31a66c32b7..84836e81dba4 100644 --- a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts +++ b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts @@ -215,6 +215,9 @@ function makeConcurrent( } catch (error: unknown) { promise = Promise.reject(error); } + // Avoid triggering the uncaught promise rejection handler in case the test errors before + // being awaited on. + promise.catch(() => {}); return spec; }; From e760a0bce9bd95a0cd3d13af45f19f3afb475420 Mon Sep 17 00:00:00 2001 From: Dmitri Gabbasov Date: Fri, 22 Oct 2021 20:48:30 +0300 Subject: [PATCH 2/2] chore: add PR link to CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21c34c5a6748..75303048506e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- `[jest-circus, jest-jasmine2]` Avoid false concurrent test failures due to unhandled promise rejections +- `[jest-circus, jest-jasmine2]` Avoid false concurrent test failures due to unhandled promise rejections ([#11987](https://github.com/facebook/jest/pull/11987)) ### Chore & Maintenance