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

🚀 Feature: Support printing each error within AggregateErrors #4982

Open
domenic opened this issue May 1, 2023 · 1 comment
Open

🚀 Feature: Support printing each error within AggregateErrors #4982

domenic opened this issue May 1, 2023 · 1 comment
Labels
status: accepting prs Mocha can use your help with this one! type: feature enhancement proposal

Comments

@domenic
Copy link
Contributor

domenic commented May 1, 2023

Is your feature request related to a problem or a nice-to-have?? Please describe.

Sometimes I'm using Mocha to test something which can fail multiple ways. This is what AggregateError is designed for.

However, when an AggregateError is thrown in a Mocha test, nothing nice prints:

it("should work with AggregateErrors", () => {
  const err1 = new Error("1");
  const err2 = new Error("2");
  const aggErr = new AggregateError([err1, err2], "2 errors");

  throw aggErr;
});

gives

  1) should work with AggregateErrors:
     AggregateError: 2 errors
      at Context.<anonymous> (x.test.js:4:18)
      at process.processImmediate (node:internal/timers:478:21)

Describe the solution you'd like

It should probably print something for each error, as well as the AggregateError itself. Maybe

     AggregateError: 2 errors
      at Context.<anonymous> (x.test.js:4:18)
      at process.processImmediate (node:internal/timers:478:21)

      Error: 1
        at Context.<anonymous> (x.test.js:2:16)
        at process.processImmediate (node:internal/timers:478:21)

      Error: 2
        at Context.<anonymous> (x.test.js:3:16)
        at process.processImmediate (node:internal/timers:478:21)

Describe alternatives you've considered

I tried making a fake AggregateError:

it("should work with fake AggregateErrors", () => {
  const err1 = new Error("1");

  const err2 = new Error("2");

  const aggErr = new Error(`2 errors:\n\n${err1.stack}\n\n${err2.stack}`);

  throw aggErr;
});

This seems to confuse Mocha's stack trace defection code and result in printing the error twice:

  1) should work with fake AggregateErrors:
     2 errors:

Error: 1
    at Context.<anonymous> (C:\Users\Domenic\Dropbox\Programming\WIP\jest-aggregateerror-test\x.test.js:2:16)
    at callFn (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runnable.js:366:21)
    at Runnable.run (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runnable.js:354:5)
    at Runner.runTest (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:666:10)
    at C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:789:12
    at next (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:581:14)
    at C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:591:7
    at next (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:474:14)
    at Immediate._onImmediate (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:559:5)
    at process.processImmediate (node:internal/timers:478:21)

Error: 2
    at Context.<anonymous> (C:\Users\Domenic\Dropbox\Programming\WIP\jest-aggregateerror-test\x.test.js:4:16)
    at callFn (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runnable.js:366:21)
    at Runnable.run (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runnable.js:354:5)
    at Runner.runTest (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:666:10)
    at C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:789:12
    at next (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:581:14)
    at C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:591:7
    at next (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:474:14)
    at Immediate._onImmediate (C:\Users\Domenic\AppData\Local\npm-cache\_npx\508606763866ae01\node_modules\mocha\lib\runner.js:559:5)
    at process.processImmediate (node:internal/timers:478:21)
  Error: 2 errors:

  Error: 1
      at Context.<anonymous> (x.test.js:2:16)
      at process.processImmediate (node:internal/timers:478:21)

  Error: 2
      at Context.<anonymous> (x.test.js:4:16)
      at process.processImmediate (node:internal/timers:478:21)
      at Context.<anonymous> (x.test.js:6:18)
      at process.processImmediate (node:internal/timers:478:21)

This is my current best solution but it's pretty noisy to get duplicate errors.

@domenic domenic added the type: feature enhancement proposal label May 1, 2023
@voxpelli
Copy link
Member

Agree on the need for this 👍

Similar to the need for a better support for Error Causes: #4829

@JoshuaKGoldberg JoshuaKGoldberg changed the title Support printing AggregateErrors sensibly 🚀 Feature: Support printing AggregateErrors Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg changed the title 🚀 Feature: Support printing AggregateErrors 🚀 Feature: Support printing each error within AggregateErrors Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg added the status: accepting prs Mocha can use your help with this one! label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepting prs Mocha can use your help with this one! type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

3 participants