Skip to content

fix: print AggregateError to display #15346

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

Merged
merged 3 commits into from
May 22, 2025
Merged

Conversation

BondarenkoAlex
Copy link
Contributor

@BondarenkoAlex BondarenkoAlex commented Oct 17, 2024

Closes #14704

Test plan

test('AggregateError', async () => {
    const fetchFromApi1 = Promise.reject(new Error('API 1 failed'));
    const fetchFromApi2 = Promise.reject(new Error('API 2 failed'));
    const promiseAny = Promise.any([fetchFromApi1, fetchFromApi2]);

    expect(promiseAny).rejects.toThrow("Error");
})

Console before change:

const innerErr = new JestAssertionError();
                     ^

JestAssertionError: expect(received).rejects.toThrow(expected)

Expected substring: "Error"
Received message:   "All promises were rejected"


    at Object.toThrow (expect/build/index.js:218:22)
    at Object.<anonymous> (index-aggregate-2.test.js:10:30)
    at Promise.then.completed (jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (jest-circus/build/utils.js:231:10)
    at _callCircusTest (jest-circus/build/run.js:316:40)
    at _runTest (jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (jest-circus/build/run.js:126:9)
    at run (jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (jest-runner/build/runTest.js:367:16)
    at runTest (jest-runner/build/runTest.js:444:34) {
  matcherResult: {
    message: '\x1B[2mexpect(\x1B[22m\x1B[31mreceived\x1B[39m\x1B[2m).\x1B[22mrejects\x1B[2m.\x1B[22mtoThrow\x1B[2m(\x1B[22m\x1B[32mexpected\x1B[39m\x1B[2m)\x1B[22m\n' +
      '\n' +
      'Expected substring: \x1B[32m"Error"\x1B[39m\n' +
      'Received message:   \x1B[31m"All promises were rejected"\x1B[39m\n' +
      '\n',
    pass: false
  }
}

Console after change:

const innerErr = new JestAssertionError();
                     ^

JestAssertionError: expect(received).rejects.toThrow(expected)

Expected substring: "Error"
Received message:   "All promises were rejected"
  ● Test suite failed to run

    AggregateError: All promises were rejected

    Errors contained in AggregateError:
     API 1 failed

          3 |
          4 | test('AggregateError', async () => {
        > 5 |     const fetchFromApi1 = Promise.reject(new Error('API 1 failed'));
            |                                          ^
          6 |     const fetchFromApi2 = Promise.reject(new Error('API 2 failed'));
          7 |     const promiseAny = Promise.any([fetchFromApi1, fetchFromApi2]);
          8 |

          at Object.<anonymous> (index-aggregate-2.test.js:5:42)

     API 2 failed

          4 | test('AggregateError', async () => {
          5 |     const fetchFromApi1 = Promise.reject(new Error('API 1 failed'));
        > 6 |     const fetchFromApi2 = Promise.reject(new Error('API 2 failed'));
            |                                          ^
          7 |     const promiseAny = Promise.any([fetchFromApi1, fetchFromApi2]);
          8 |
          9 |     expect(promiseAny).rejects.toThrow("Error");

          at Object.<anonymous> (index-aggregate-2.test.js:6:42)


    at Object.toThrow (/expect/build/index.js:218:22)
    at Object.toThrow (/index-aggregate-2.test.js:9:32)
    at Promise.then.completed (/jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/jest-circus/build/utils.js:231:10)
    at _callCircusTest (/jest-circus/build/run.js:316:40)
    at _runTest (/jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (/jest-circus/build/run.js:126:9)
    at run (/jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (/jest-runner/build/runTest.js:367:16)
    at runTest (/jest-runner/build/runTest.js:444:34) {
  matcherResult: {
    message: '\x1B[2mexpect(\x1B[22m\x1B[31mreceived\x1B[39m\x1B[2m).\x1B[22mrejects\x1B[2m.\x1B[22mtoThrow\x1B[2m(\x1B[22m\x1B[32mexpected\x1B[39m\x1B[2m)\x1B[22m\n' +
      '\n' +
      'Expected substring: \x1B[32m"Error"\x1B[39m\n' +
      'Received message:   \x1B[31m"All promises were rejected"\x1B[39m\n' +
      '  \x1B[1m● \x1B[22mTest suite failed to run\n' +
      '\n' +
      '    AggregateError: All promises were rejected\n' +
      '\n' +
      '    Errors contained in AggregateError:\n' +
      '     API 1 failed\n' +
      '\n' +
      '        \x1B[0m \x1B[90m 3 |\x1B[39m\x1B[0m\n' +
      "        \x1B[0m \x1B[90m 4 |\x1B[39m test(\x1B[32m'AggregateError'\x1B[39m\x1B[33m,\x1B[39m \x1B[36masync\x1B[39m () \x1B[33m=>\x1B[39m {\x1B[0m\n" +
      "        \x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 5 |\x1B[39m     \x1B[36mconst\x1B[39m fetchFromApi1 \x1B[33m=\x1B[39m \x1B[33mPromise\x1B[39m\x1B[33m.\x1B[39mreject(\x1B[36mnew\x1B[39m \x1B[33mError\x1B[39m(\x1B[32m'API 1 failed'\x1B[39m))\x1B[33m;\x1B[39m\x1B[0m\n" +
      '        \x1B[0m \x1B[90m   |\x1B[39m                                          \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
      "        \x1B[0m \x1B[90m 6 |\x1B[39m     \x1B[36mconst\x1B[39m fetchFromApi2 \x1B[33m=\x1B[39m \x1B[33mPromise\x1B[39m\x1B[33m.\x1B[39mreject(\x1B[36mnew\x1B[39m \x1B[33mError\x1B[39m(\x1B[32m'API 2 failed'\x1B[39m))\x1B[33m;\x1B[39m\x1B[0m\n" +
      '        \x1B[0m \x1B[90m 7 |\x1B[39m     \x1B[36mconst\x1B[39m promiseAny \x1B[33m=\x1B[39m \x1B[33mPromise\x1B[39m\x1B[33m.\x1B[39many([fetchFromApi1\x1B[33m,\x1B[39m fetchFromApi2])\x1B[33m;\x1B[39m\x1B[0m\n' +
      '        \x1B[0m \x1B[90m 8 |\x1B[39m\x1B[0m\n' +
      '\n' +
      '          \x1B[2mat Object.<anonymous> (\x1B[22mindex-aggregate-2.test.js\x1B[2m:5:42)\x1B[22m\n' +
      '\n' +
      '     API 2 failed\n' +
      '\n' +
      "        \x1B[0m \x1B[90m 4 |\x1B[39m test(\x1B[32m'AggregateError'\x1B[39m\x1B[33m,\x1B[39m \x1B[36masync\x1B[39m () \x1B[33m=>\x1B[39m {\x1B[0m\n" +
      "        \x1B[0m \x1B[90m 5 |\x1B[39m     \x1B[36mconst\x1B[39m fetchFromApi1 \x1B[33m=\x1B[39m \x1B[33mPromise\x1B[39m\x1B[33m.\x1B[39mreject(\x1B[36mnew\x1B[39m \x1B[33mError\x1B[39m(\x1B[32m'API 1 failed'\x1B[39m))\x1B[33m;\x1B[39m\x1B[0m\n" +
      "        \x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 6 |\x1B[39m     \x1B[36mconst\x1B[39m fetchFromApi2 \x1B[33m=\x1B[39m \x1B[33mPromise\x1B[39m\x1B[33m.\x1B[39mreject(\x1B[36mnew\x1B[39m \x1B[33mError\x1B[39m(\x1B[32m'API 2 failed'\x1B[39m))\x1B[33m;\x1B[39m\x1B[0m\n" +
      '        \x1B[0m \x1B[90m   |\x1B[39m                                          \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
      '        \x1B[0m \x1B[90m 7 |\x1B[39m     \x1B[36mconst\x1B[39m promiseAny \x1B[33m=\x1B[39m \x1B[33mPromise\x1B[39m\x1B[33m.\x1B[39many([fetchFromApi1\x1B[33m,\x1B[39m fetchFromApi2])\x1B[33m;\x1B[39m\x1B[0m\n' +
      '        \x1B[0m \x1B[90m 8 |\x1B[39m\x1B[0m\n' +
      '        \x1B[0m \x1B[90m 9 |\x1B[39m     expect(promiseAny)\x1B[33m.\x1B[39mrejects\x1B[33m.\x1B[39mtoThrow(\x1B[32m"Error"\x1B[39m)\x1B[33m;\x1B[39m\x1B[0m\n' +
      '\n' +
      '          \x1B[2mat Object.<anonymous> (\x1B[22mindex-aggregate-2.test.js\x1B[2m:6:42)\x1B[22m\n' +
      '\n',
    pass: false
  }
}

Copy link

netlify bot commented Oct 17, 2024

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6b57fd0
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/682eddfa216b9000081e8c37
😎 Deploy Preview https://deploy-preview-15346--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@BondarenkoAlex BondarenkoAlex changed the title fix: print AggregateErrors to display fix: print AggregateError to display Oct 17, 2024
@BondarenkoAlex BondarenkoAlex marked this pull request as ready for review October 17, 2024 19:26
return '';
} else {
const config = {
rootDir: process.cwd(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot use this as expect is meant to work in the browser (and rootDir might not even be cwd).

If we are to do this, we'll need to pass the config object somehow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just carry this from line 462.

Why do not it work here?

Copy link

This PR is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label May 21, 2025
@BondarenkoAlex
Copy link
Contributor Author

up

Co-authored-by: Casper Juel <[email protected]>
@cpojer cpojer merged commit 73c5fe9 into jestjs:main May 22, 2025
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Errors in AggregateError not displayed properly
4 participants