jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.19k stars 6.46k forks source link

Print stack trace and all own enumerable properties of unexpected errors thrown in a test #10187

Closed InExtremaRes closed 2 years ago

InExtremaRes commented 4 years ago

πŸš€ Feature Proposal

When an unexpected exception is thrown inside a test (or beforAll, etc.), print to the console all own enumerable properties of the error, not just the message (and the stacktrace when available).

Motivation

When an operation inside a test (or hook like beforeEach) throws an exception that is not caught or expected, Jest reports the test as failed and print the message of the to the console, but often those errors have properties with useful information to find the culprit. For example Node.js errors usually have a code property, or database drivers can inform the DB error and the query, to name a few; all of this besides the more common stack property.

Since a developer is not expecting the operation to fail (otherwise there would be an expectation for it) it might not be obvious why it is failing or where the error was originated only looking at the message. More often than not I've had to manually catch and print the error to the console to be able to see the properties (or start a debugger). This would be a lot easier and faster if those properties were printed by Jest itself.

Example

I think the idea is pretty clear, but this is what I expect:

test('can go to school and return with homework', () => {
    const homework = goToSchoolWithoutBreakfast();

    expect(homework.daysWithoutSleep).toBe(2);
});

But something unexpected happen... The console then shows:

● can go to school and return with homework

  Error: I don't feel so good...
  {
    causedBy: 'eat to many pizzas',
    timeToLive: '3 seconds',
    important: 'The key to unlock my safebox and find the <request aborted prematurely>'
  }
  at pizzaStore:3:14
  at Script.runInThisContext (vm.js:120:20)
  [...]
  at REPLServer.Interface._line (readline.js:658:8)

instead of just:

● can go to school and return with homework

  Error: I don't feel so good...
    at pizzaStore:3:14
    at Script.runInThisContext (vm.js:120:20)
    [...]
    at REPLServer.Interface._line (readline.js:658:8)

Pitch

I really think this should be the default behaviour of Jest, at least in jest-circus, but I'm not very familiar with custom reporters so maybe I'm wrong.

SimenB commented 4 years ago

Blocked by #9496 for now, as reporters currently don't have access to this info. I do like the idea, though! Not sure where to put that information in the reporter though

github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] commented 2 years ago

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.