Skip to content

Commit

Permalink
fix: avoid unhandled promise rejections when concurrent tests fail (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-gb authored Nov 29, 2021
1 parent 5cd75f4 commit 9d14c5d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixes

- `[expect]` Allow again `expect.Matchers` generic with single value ([#11986](https://github.com/facebook/jest/pull/11986))
- `[jest-circus, jest-jasmine2]` Avoid false concurrent test failures due to unhandled promise rejections ([#11987](https://github.com/facebook/jest/pull/11987))
- `[jest-config]` Add missing `slash` dependency to `package.json` ([#12080](https://github.com/facebook/jest/pull/12080))
- `[jest-core]` Incorrect detection of open ZLIB handles ([#12022](https://github.com/facebook/jest/pull/12022))
- `[jest-diff]` Break dependency cycle ([#10818](https://github.com/facebook/jest/pull/10818))
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(() => {});
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

0 comments on commit 9d14c5d

Please sign in to comment.