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

Better stack traces in asynchronous errors #5104

Closed
SimenB opened this issue Dec 18, 2017 · 6 comments · Fixed by #6008
Closed

Better stack traces in asynchronous errors #5104

SimenB opened this issue Dec 18, 2017 · 6 comments · Fixed by #6008

Comments

@SimenB
Copy link
Member

SimenB commented Dec 18, 2017

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
There are three cases (that I know of) when a test is failing, and we don't point back to the user's own code.

test('some async test', () => {
  return expect(Promise.resolve('foo')).resolves.toBe('bar');
});

test('hasAssertions', () => {
  expect.hasAssertions();

  return Promise.resolve('foobar');
});

test('assertions', () => {
  expect.assertions(42);

  return Promise.resolve('foobar');
});

This renders the following errors:

image

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

See above.

What is the expected behavior?
For the stack trace to point back to the failing test. In the first case, I would expect it to point to the line of the expect, and in the the two other cases, to the lines specifying assertions.

I think the only way to achieve this is to somehow store an error whenever those functions are resolved, and use its stack on failure, instead of creating a new Error within Jest itself on failure. As long as we don't inspect the stack trace (unless we need it for an error), the overhead should be minimal.

If we get a good stack trace, we automatically get the codeframe as well.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

[email protected], [email protected]

@SimenB
Copy link
Member Author

SimenB commented Dec 18, 2017

For completeness, what I want as stack for all of those are:

  ● some async test

    expect(received).toBe(expected)

    Expected value to be (using Object.is):
      "bar"
    Received:
      "foo"

      1 | test('some async test', () => {
    > 2 |   return expect(Promise.resolve('foo')).resolves.toBe('bar');
      3 | });
      4 |
      5 |test('hasAssertions', () => {

      at Object.<anonymous>.test (test.js:2:10)

  ● hasAssertions

    expect.hasAssertions()

    Expected at least one assertion to be called but received none.

      4 |
      5 | test('hasAssertions', () => {
    > 6 |   expect.hasAssertions();
      7 |
      8 |   return Promise.resolve('foobar');
      9 |});

      at Object.<anonymous>.test (test.js:6:10)

  ● assertions

    expect.assertions(42)

    Expected 42 assertions to be called but received zero assertion calls.

      10 |
      11 | test('assertions', () => {
    > 12 |   expect.assertions(42);
      13 |
      14 |   return Promise.resolve('foobar');
      15 |});

      at Object.<anonymous>.test (test.js:12:10)

@SimenB
Copy link
Member Author

SimenB commented Dec 19, 2017

Another case is timeouts.

image
We should at least point back to the test - maybe the it itself

@stopsopa
Copy link

Same here: #5902

@SimenB
Copy link
Member Author

SimenB commented Apr 16, 2018

Opened up #6008. Combined with #5997 I think that covers all cases reported here except for throw 'something' or await Promise.reject('something') - that is thrown or rejected non-errors. I think we might want to point back to the test itself there (like with spec timeouts in my PR)? We have no way of accessing the real stack, as the rejection will always be async

@SimenB
Copy link
Member Author

SimenB commented Apr 17, 2018

For those following along, a new alpha is out: [email protected]. Please give it a whirl and report any remaining issues with traces 🙂

@github-actions
Copy link

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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants