Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions packages/jest-circus/src/__tests__/asyncError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

describe('async error handling', () => {
it('does not crash when asyncError is undefined and a plain object is thrown', done => {
const {test} = require('../');

test('inner test', (innerDone: any) => {
// simulate async error path (where asyncError can be undefined)
Promise.resolve().then(() => {
throw Object.assign(new Error('Forbidden'), {status: 403});
});

innerDone();
});

done();
});
});
7 changes: 6 additions & 1 deletion packages/jest-circus/src/formatNodeAssertErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ const formatNodeAssertErrors = (
error = asyncError;
} else if (originalError.stack) {
error = originalError;
} else {
} else if (asyncError) {
error = asyncError;

error.message =
originalError.message ||
`thrown: ${prettyFormat(originalError, {maxDepth: 3})}`;
} else {
error = new Error(
originalError.message ||
`thrown: ${prettyFormat(originalError, {maxDepth: 3})}`,
);
}
} else {
error = errors;
Expand Down
7 changes: 5 additions & 2 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,12 @@ const _getError = (
return error;
}

asyncError.message = `thrown: ${prettyFormat(error, {maxDepth: 3})}`;
if (asyncError) {
asyncError.message = `thrown: ${prettyFormat(error, {maxDepth: 3})}`;
return asyncError;
}

return asyncError;
return new Error(`thrown: ${prettyFormat(error, {maxDepth: 3})}`);
};

const getErrorStack = (error: Error): string =>
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-types/src/Circus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ export type DescribeBlock = {
tests: Array<TestEntry>;
};

export type TestError = Exception | [Exception | undefined, Exception]; // the error from the test, as well as a backup error for async
export type TestError =
| Exception
| [Exception | undefined, Exception | undefined]; // the error from the test, as well as a backup error for async

export type TestEntry = {
type: 'test';
Expand Down
Loading