jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.17k stars 6.45k forks source link

failing before hooks still cause tests to run #11060

Open Nokel81 opened 3 years ago

Nokel81 commented 3 years ago

🐛 Bug Report

A before hook (either set by beforeAll or beforeEach) that fails (either by throwing or by rejecting) does not prevent a test from running.

To Reproduce

  1. Have a beforeEach hook always throw
  2. Have a test that always passes (and logs some output)
  3. Run tests

Expected behaviour

The test fails with "beforeEach hook failed" or something similar.

Link to repl or repo (highly encouraged)

https://repl.it/talk/share/jest-before-hook/119943

envinfo

  System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 12.20.1 - ~/.nvm/versions/node/v12.20.1/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.8 - ~/repos/lens/node_modules/.bin/npm
  npmPackages:
    jest: ^26.0.1 => 26.0.1

Though the above repl is configured to be the latest version of jest and it still shows this problem.

Latchumi commented 3 years ago

Tests are not waiting for beforeAll hooks to complete. Jest started to run the test parallely.

async function consoleIt(n, resolve) {
  console.log('Logging', n);
  resolve();
}

async function initializeSuite() {
  console.log('log0');
  await new Promise((resolve) => setTimeout(consoleIt.bind(null, 1, resolve), 20));
  console.log('log1');
  await new Promise((resolve) => setTimeout(consoleIt.bind(null, 2, resolve), 10));
  console.log('log2');
  await new Promise((resolve) => setTimeout(consoleIt.bind(null, 3, resolve), 50));
  console.log('log3');
  await new Promise((resolve) => setTimeout(consoleIt.bind(null, 4, resolve), 30));
  console.log('log4');
}

beforeAll(initializeSuite, 500);

describe('Sample test case describe', () => {
  it('Sample test case', async () => {
    console.log('******executed');
  });
});

Expected output is:

Logging 1
 log1
 Logging 2
 log2
 Logging 3
 log3
 Logging 4
 log4
 ******executed

But its not executing all console statements. Especially when the timeout is 0, it works absolutely fine. Otherwise its not waiting for the beforeAll to finish its process. It executes the test case, but not printing the log within test case as well

Nokel81 commented 3 years ago

So it seems more broken than I thought

Latchumi commented 3 years ago

@Nokel81 any workaround you have for the issue you are facing?

Nokel81 commented 3 years ago

I have not found any, the tests seem to fail as well but they take longer to fail.

github-actions[bot] commented 1 year ago

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

Nokel81 commented 1 year ago

Still very relavent

github-actions[bot] commented 8 months ago

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

Nokel81 commented 8 months ago

Still an issue