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

feat(jest-message-util): add support for AggregateError #13946

Merged
merged 8 commits into from Feb 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- `[jest-cli, jest-config, @jest/core, jest-haste-map, @jest/reporters, jest-runner, jest-runtime, @jest/types]` Add `workerThreads` configuration option to allow using [worker threads](https://nodejs.org/dist/latest/docs/api/worker_threads.html) for parallelization ([#13939](https://github.com/facebook/jest/pull/13939))
- `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937))
- `[jest-message-util]` Add support for [AggregateErrors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946))

### Fixes

Expand Down
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
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
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