Skip to content

Better stack traces in asynchronous errors #5104

@SimenB

Description

@SimenB

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]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions