Skip to content

Commit

Permalink
feat(jest-message-util): add support for AggregateError (#13946)
Browse files Browse the repository at this point in the history
  • Loading branch information
brodo committed Feb 23, 2023
1 parent 4e02aef commit a1ceb9a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- `[jest-changed-files]` Support Sapling ([#13941](https://github.com/facebook/jest/pull/13941))
- `[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-message-util]` Add support for [AggregateError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946))
- `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937))

### Fixes
Expand Down
Expand Up @@ -71,6 +71,25 @@ exports[`no stack 1`] = `
"
`;
exports[`on node >=15.0.0 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>:441:24)</intensity>
Errors contained in AggregateError:
Err 1
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:442:9)</intensity>
Err 2
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:443:9)</intensity>
"
`;
exports[`retains message in babel code frame error 1`] = `
"<bold><red> <bold>● </intensity><bold>Babel test</color></intensity>
Expand Down Expand Up @@ -118,12 +137,12 @@ exports[`should return the error cause if there is one 1`] = `
Test exception
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:418:17)</intensity>
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:419:17)</intensity>
Cause:
Cause Error
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:421:17)</intensity>
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:422:17)</intensity>
"
`;
25 changes: 25 additions & 0 deletions packages/jest-message-util/src/__tests__/messages.test.ts
Expand Up @@ -9,6 +9,7 @@
import {readFileSync} from 'graceful-fs';
import slash = require('slash');
import tempy = require('tempy');
import {onNodeVersions} from '@jest/test-utils';
import {
formatExecError,
formatResultsErrors,
Expand Down Expand Up @@ -431,3 +432,27 @@ it('should return the error cause if there is one', () => {
);
expect(message).toMatchSnapshot();
});

// TODO remove this wrapper when the lowest supported Node version is v16
onNodeVersions('>=15.0.0', () => {
it('should return the inner errors of an AggregateError', () => {
// 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

0 comments on commit a1ceb9a

Please sign in to comment.