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

[Feature]: expect.hasAssertions() should not verify when the test is already failing #14084

Closed
mockdeep opened this issue Apr 18, 2023 · 5 comments

Comments

@mockdeep
Copy link

🚀 Feature Proposal

expect.hasAssertions() should only report an issue when the test is passing.

Motivation

When the JavaScript code fails due to an error, it causes double output with Jest. For example, the following test:

it('fails the test', () => {
  throw new Error('woops!');

  expect(true).toBe(true);
});

Produces this output:

  ● .CONTENT_NUMBER_REGEX › fails the test

    woops!

       5 |
       6 |   fit('fails the test', () => {
    >  7 |     throw new Error('woops!');
         |           ^
       8 |
       9 |     expect(true).toBe(true);
      10 |   });

      at Object.<anonymous> (spec/javascript/constants_spec.ts:7:11)
          at runMicrotasks (<anonymous>)

  ● .CONTENT_NUMBER_REGEX › fails the test

    expect.hasAssertions()

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

      44 |
      45 | beforeEach(() => {
    > 46 |   expect.hasAssertions();
         |          ^
      47 | });
      48 |
      49 | afterEach(() => {

      at Object.<anonymous> (spec/javascript/test_helper.ts:46:10)
          at runMicrotasks (<anonymous>)

If you're running your tests in a tiny VSCode pane, all you see is the hasAssertions() error which is a red herring. You need to scroll up to see the real error.

Example

Instead of displaying the hasAssertions error, when the test is already failing we should only see the relevant failure:

  ● .CONTENT_NUMBER_REGEX › fails the test

    woops!

       5 |
       6 |   fit('fails the test', () => {
    >  7 |     throw new Error('woops!');
         |           ^
       8 |
       9 |     expect(true).toBe(true);
      10 |   });

      at Object.<anonymous> (spec/javascript/constants_spec.ts:7:11)
          at runMicrotasks (<anonymous>)

Pitch

Because the hasAssertions() option is part of the Jest core platform, it seems like this makes sense to address there as well.

@github-actions
Copy link

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

@github-actions github-actions bot added the Stale label May 18, 2023
@github-actions
Copy link

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 17, 2023
@github-actions
Copy link

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

@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 Jul 18, 2023
@SimenB
Copy link
Member

SimenB commented Feb 20, 2024

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