Skip to content

🚀 Feature: Support printing each error within AggregateErrors #4982

Open
@domenic

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions