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

fix: stop printing the same error for each individual test caused by beforeAll when it fails #10004

Closed
wants to merge 2 commits into from

Conversation

vldmrkl
Copy link

@vldmrkl vldmrkl commented May 8, 2020

Summary

Closes #9901

From my research, I found that the call of addErrorToEachTestUnderDescribe was causing this bug (line 160):
https://github.com/facebook/jest/blob/0a4e4a1657b42b21ebf3a969fbf3bbdd28ca930f/packages/jest-circus/src/eventHandler.ts#L154-L170

Removing this function call would stop printing error messages from beforeAll failure for each individual test, however, test suit would pass even though beforeAll failed. That's why I had to add an error to state.unhandledErrors, so it fails the test suit and prints an error message for beforeAll failure.

Since the code is the same as as-if statement for afterAll type, I joined type === 'beforeAll' and type === 'afterAll'.

These changes haven't broken any existing test in the project, and it looks like it is much less confusing comparing to the previous version, though I would like to get confirmation that this is the expected behaviour.

Test plan

Let's reuse the example from the issue description and see a new behaviour that this bug fix provides.

Example:

describe('test that a 3rd party API remains consistent', () => {
  beforeAll(() => expect('login').toBe('successful')); // this will fail
  test('API function 1', () => expect(1).toBe(1)); // each...
  test('API function 2', () => expect(2).toBe(2)); // ...of these...
  test('API function 3', () => expect(3).toBe(3)); // ...will be reported as failed too
});

New behaviour:

 FAIL  ./beforeAllFails.test.js


  ● Test suite failed to run

    expect(received).toBe(expected) // Object.is equality

    Expected: "successful"
    Received: "login"

      1 | describe('test that a 3rd party API remains consistent', () => {
    > 2 |   beforeAll(() => expect('login').toBe('successful'));
        |                                   ^
      3 |   test('API function 1', () => expect(1).toBe(1));
      4 |   test('API function 2', () => expect(2).toBe(2)); // ...of these...
      5 |   test('API function 3', () => expect(3).toBe(3)); // ...will be reported as failed too

      at beforeAllFails.test.js:2:35

Test Suites: 1 failed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        3.559 s

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has broken 2 tests. To run with circus locally, do JEST_CIRCUS=1 yarn jest.

Also, I think we should have better messages. Instead of unhandled we can have a new hookErrors field or something? That might be considered breaking for people consuming our events though, not sure.

This also needs to be covered by explicit tests 🙂

@thernstig
Copy link
Contributor

thernstig commented May 20, 2020

@vldmrkl It says Tests: 3 passed, 3 total, should this not say Tests: 0 passed, 3 total? Maybe that is a separate issue though, related to #9911 ?

@github-actions
Copy link

github-actions bot commented Sep 8, 2022

This PR is stale because it has been open 1 year 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 Sep 8, 2022
@thernstig
Copy link
Contributor

@vldmrkl any chance you could finalize this? :)

@github-actions github-actions bot removed the Stale label Sep 9, 2022
@vldmrkl
Copy link
Author

vldmrkl commented Sep 11, 2022

@thernstig #9901 (comment)

@vldmrkl
Copy link
Author

vldmrkl commented Sep 17, 2022

Closing this PR in favour of #13273

@vldmrkl vldmrkl closed this Sep 17, 2022
@github-actions
Copy link

This pull request 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 Oct 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Jest should avoid printing the individual tests if beforeAll fails
4 participants