question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

failing beforeAll() causes even passing tests in the scope to fail

See original GitHub issue

šŸ› Bug Report

Iā€™m looking for the best practice for aborting a describe block if the test setup fails. For example, when testing an API, if authenticating fails, itā€™s pointless to run any other tests.

As suggested in the issue template, I searched StackOverflow first, where I found this incomplete answer, advising to place the initialization code in a beforeAll block. Hence, raising the issue here because

  1. I havenā€™t seen anything documenting the practice of returning failure from beforeAll
  2. If a beforeAll block fails, tests in that describe block are still run, and they fail, even if otherwise they would pass!

To Reproduce

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 fail too
});

Expected behavior

Jest should report that the beforeAll() failed, and bail the describe scope without executing further tests in it. If itā€™s somehow intended behavior that all tests should still be executed, and still marked as failed (which I find odd), this should be documented under beforeAll and in the Setup and Teardown guide. Other tests in the file, outside of the failing block, should still be executed.

Link to repl or repo (highly encouraged)

https://repl.it/@DanDascalescu/beforeAll-failure-should-bail-the-test

Run npx envinfo --preset jest

  System:
    OS: Linux 4.15 Ubuntu 16.04.4 LTS (Xenial Xerus)
    CPU: x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
  Binaries:
    Node: 10.5.0 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/bin/yarn
    npm: 6.2.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^23.4.1 => 23.4.1 

See also

#6527

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:26
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
SimenBcommented, May 6, 2019

To my eyes this just looks like an issue with the reporting. beforeAll is only run once and none of the other tests actually execute, but they all fail with the same reason, and they all print the same error.

We should probably avoid printing the individual tests if beforeAll fails. Right now the implementation just checks if weā€™ve had an error and if yes, print it and fail the test without actually executing it

3reactions
chrismwendtcommented, Apr 9, 2019

jest-circus seems to not fix this. All 3 tests still run and fail.

Hereā€™s the command I ran:

$ yarn run jest --testRunner=jest-circus/runner --bail

Am I missing a configuration value somewhere?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setup and Teardown - Jest
If you have a test that often fails when it's run as part of a larger suite, but doesn't fail when you run...
Read more >
JUnit 5 does not execute method annotated with BeforeEach
In my case the problem was that the @Test annotation was taken from wrong import. Originally it was imported from org.junit.Test .
Read more >
Sharing fixtures - ScalaTest
withFixture(OneArgTest), Use when you want to pass the same fixture object or ... (If a test fails because of an exception, the test...
Read more >
pytest fixtures: explicit, modular, scalable
One of the things that makes pytest's fixture system so powerful, ... When a test is marked as having an error, it doesn't...
Read more >
Best Practices - Cypress Documentation
get() with data attributes. Changing the text to Save would then not cause a test failure. Cypress and Testing Library. Cypress loves the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found