Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid unhandled promise rejections when concurrent tests fail #11987

Merged
merged 6 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-circus, jest-jasmine2]` Avoid false concurrent test failures due to unhandled promise rejections ([#11987](https://github.com/facebook/jest/pull/11987))

### Chore & Maintenance

### Performance
Expand Down
9 changes: 9 additions & 0 deletions e2e/__tests__/jasmineAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
16 changes: 16 additions & 0 deletions e2e/jasmine-async/__tests__/concurrent-parallel-failure.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this doesn't mean promise will never reject, that is only true for the promise returned by catch(...).

globalsObject.test(testName, () => promise, timeout);
};

Expand Down
3 changes: 3 additions & 0 deletions packages/jest-jasmine2/src/jasmineAsyncInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down