Skip to content

Commit

Permalink
fix: print AggregateErrors to display
Browse files Browse the repository at this point in the history
Closes #14704
  • Loading branch information
BondarenkoAlex committed Oct 17, 2024
1 parent 0a0a9f7 commit 7853063
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"jest-jasmine-ci": "yarn jest-jasmine --color --config jest.config.ci.mjs",
"jest-coverage": "yarn jest --coverage",
"lint": "eslint . --cache --ext js,jsx,cjs,mjs,ts,tsx,md",
"lint:fix": "eslint ./packages/expect/src/toThrowMatchers.ts --cache --fix",
"lint:prettier-script": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx}\" --cache",
"lint:prettier": "yarn lint:prettier-script --write",
"lint:prettier:ci": "yarn lint:prettier-script --check",
Expand Down
30 changes: 30 additions & 0 deletions packages/expect/src/__tests__/toThrowMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,36 @@ describe('toThrow', () => {
});
});

describe('aggregate-errors', () => {
const fetchFromApi1 = Promise.reject(new Error('API 1 failed'));
const fetchFromApi2 = Promise.reject(new Error('API 2 failed'));
const promiseAny = Promise.any([fetchFromApi1, fetchFromApi2]);

Check failure on line 338 in packages/expect/src/__tests__/toThrowMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Property 'any' does not exist on type 'PromiseConstructor'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later.

test('string', () => {
jestExpect(promiseAny).rejects.toThrow('All promises were rejected');
});

test('undefined', () => {
jestExpect(promiseAny).rejects.toThrow();
});

test('asymmetricMatch', () => {
jestExpect(promiseAny).rejects.toThrow(
expect.objectContaining({
message: 'All promises were rejected',
}),
);
});

test('regexp', () => {
jestExpect(promiseAny).rejects.toThrow(/All promises were rejected/);
});

test('class', () => {
jestExpect(promiseAny).rejects.toThrow(AggregateError);

Check failure on line 361 in packages/expect/src/__tests__/toThrowMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Cannot find name 'AggregateError'.
});
});

describe('asymmetric', () => {
describe('any-Class', () => {
describe('pass', () => {
Expand Down
37 changes: 25 additions & 12 deletions packages/expect/src/toThrowMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
printReceived,
printWithType,
} from 'jest-matcher-utils';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
import {
formatExecError,
formatStackTrace,
separateMessageFromStack,
} from 'jest-message-util';
import {
printExpectedConstructorName,
printExpectedConstructorNameNot,
Expand Down Expand Up @@ -453,19 +457,28 @@ const formatReceived = (
return '';
};

const formatStack = (thrown: Thrown | null) =>
thrown === null || !thrown.isError
? ''
: formatStackTrace(
const formatStack = (thrown: Thrown | null) => {
if (thrown === null || !thrown.isError) {
return '';
} else {
const config = {
rootDir: process.cwd(),
testMatch: [],
};
const options = {
noStackTrace: false,
};
if (thrown.value instanceof AggregateError) {
return formatExecError(thrown.value, config, options);
} else {
return formatStackTrace(
separateMessageFromStack(thrown.value.stack!).stack,
{
rootDir: process.cwd(),
testMatch: [],
},
{
noStackTrace: false,
},
config,
options,
);
}
}
};

function createMessageAndCauseMessage(error: Error): string {
if (error.cause instanceof Error) {
Expand Down

0 comments on commit 7853063

Please sign in to comment.