Skip to content

Commit 9d14c5d

Browse files
authored
fix: avoid unhandled promise rejections when concurrent tests fail (#11987)
1 parent 5cd75f4 commit 9d14c5d

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixes
1010

1111
- `[expect]` Allow again `expect.Matchers` generic with single value ([#11986](https://github.com/facebook/jest/pull/11986))
12+
- `[jest-circus, jest-jasmine2]` Avoid false concurrent test failures due to unhandled promise rejections ([#11987](https://github.com/facebook/jest/pull/11987))
1213
- `[jest-config]` Add missing `slash` dependency to `package.json` ([#12080](https://github.com/facebook/jest/pull/12080))
1314
- `[jest-core]` Incorrect detection of open ZLIB handles ([#12022](https://github.com/facebook/jest/pull/12022))
1415
- `[jest-diff]` Break dependency cycle ([#10818](https://github.com/facebook/jest/pull/10818))

e2e/__tests__/jasmineAsync.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,13 @@ describe('async jasmine', () => {
168168

169169
expect(result.exitCode).toBe(0);
170170
});
171+
172+
it('works when another test fails while one is running', () => {
173+
const {json} = runWithJson('jasmine-async', [
174+
'concurrent-parallel-failure.test.js',
175+
]);
176+
expect(json.numTotalTests).toBe(2);
177+
expect(json.numPassedTests).toBe(1);
178+
expect(json.numFailedTests).toBe(1);
179+
});
171180
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
it.concurrent('Good Test', async () => {
11+
await new Promise(r => setTimeout(r, 100));
12+
});
13+
14+
it.concurrent('Bad Test', async () => {
15+
expect('a').toBe('b');
16+
});

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ export const initialize = async ({
9393
// that will result in this test to be skipped, so we'll be executing the promise function anyway,
9494
// even if it ends up being skipped.
9595
const promise = mutex(() => testFn());
96+
// Avoid triggering the uncaught promise rejection handler in case the test errors before
97+
// being awaited on.
98+
promise.catch(() => {});
9699
globalsObject.test(testName, () => promise, timeout);
97100
};
98101

packages/jest-jasmine2/src/jasmineAsyncInstall.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ function makeConcurrent(
215215
} catch (error: unknown) {
216216
promise = Promise.reject(error);
217217
}
218+
// Avoid triggering the uncaught promise rejection handler in case the test errors before
219+
// being awaited on.
220+
promise.catch(() => {});
218221

219222
return spec;
220223
};

0 commit comments

Comments
 (0)