Skip to content

Commit

Permalink
feat(jest-message-util): add support for AggregateErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
brodo committed Feb 22, 2023
1 parent e2196ca commit 76d1b8c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,22 @@ exports[`should return the error cause if there is one 1`] = `
"
`;
exports[`should return the inner errors of an AggregateError 1`] = `
" <bold>● </intensity>Test suite failed to run
AggregateError:
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:439:22)</intensity>
Errors contained in AggregateError:
Err 1
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:440:7)</intensity>
Err 2
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:441:7)</intensity>
"
`;
22 changes: 22 additions & 0 deletions packages/jest-message-util/src/__tests__/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,25 @@ it('should return the error cause if there is one', () => {
);
expect(message).toMatchSnapshot();
});

it('should return the inner errors of an AggregateError', () => {
// TODO remove this if when the lowest supported Node version is 15.0.0
// See https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V15.md#v8-86---35415
if (AggregateError) {
const aggError = new AggregateError([
new Error('Err 1'),
new Error('Err 2'),
]);
const message = formatExecError(
aggError,
{
rootDir: '',
testMatch: [],
},
{
noStackTrace: false,
},
);
expect(message).toMatchSnapshot();
}
});
23 changes: 22 additions & 1 deletion packages/jest-message-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export const formatExecError = (

let message, stack;
let cause = '';
const subErrors = [];

if (typeof error === 'string' || !error) {
error || (error = 'EMPTY ERROR');
Expand Down Expand Up @@ -172,6 +173,20 @@ export const formatExecError = (
cause += `${prefix}${formatted}`;
}
}
if ('errors' in error && Array.isArray(error.errors)) {
for (const subError of error.errors) {
subErrors.push(
formatExecError(
subError,
config,
options,
testPath,
reuseMessage,
true,
),
);
}
}
}
if (cause !== '') {
cause = indentAllLines(cause);
Expand Down Expand Up @@ -210,8 +225,14 @@ export const formatExecError = (
messageToUse = `${EXEC_ERROR_MESSAGE}\n\n${message}`;
}
const title = noTitle ? '' : `${TITLE_INDENT + TITLE_BULLET}`;
const subErrorStr =
subErrors.length > 0
? indentAllLines(
`\n\nErrors contained in AggregateError:\n${subErrors.join('\n')}`,
)
: '';

return `${title + messageToUse + stack + cause}\n`;
return `${title + messageToUse + stack + cause + subErrorStr}\n`;
};

const removeInternalStackEntries = (
Expand Down

0 comments on commit 76d1b8c

Please sign in to comment.