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

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

Closed
InExtremaRes opened this issue Jun 22, 2020 · 4 comments

Comments

@InExtremaRes
Copy link
Contributor

InExtremaRes commented Jun 22, 2020

🚀 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
Copy link
Member

SimenB commented Jun 23, 2020

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
Copy link

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 github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

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
Copy link

github-actions bot commented May 5, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants