jest icon indicating copy to clipboard operation
jest copied to clipboard

jest-circus beforeAll hook behaviour on failure

Open njmaeff opened this issue 3 years ago • 10 comments

🐛 Bug Report

When you have a failing beforeAll hook, all remaining beforeAll hooks will still run. Contrast that to beforeEach which after failing will skip the rest of the beforeEach hooks and skip the test.

To Reproduce

Steps to reproduce the behavior:

Expected behavior

I think after the first beforeAll failure, every other beforeAll hook and child test should be skipped / failed.

Link to repl or repo (highly encouraged)

import wrap from 'jest-snapshot-serializer-raw';
import {runTest} from 'jest-circus/src/__mocks__/testUtils';

test('a failing beforeAll will not skip other beforeAll hooks', () => {
    const {stdout} = runTest(`
    describe('test suite beforeAll', () => {

    beforeAll(() => {
        console.log('beforeAll 1 runs')
        throw new Error('beforeAll 1');
    });

    beforeAll(() => {
        console.log('beforeAll 2 runs')
        throw new Error('beforeAll 2');
    });

    test('should not run', () => {
        console.log('the test ran')
    });

});
  `);

    expect(wrap(stdout)).toMatchSnapshot();
});

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a failing beforeAll will not skip other beforeAll hooks 1`] = `
Object {
  Symbol(jest-snapshot-serializer-raw): "start_describe_definition: test suite beforeAll
add_hook: beforeAll
add_hook: beforeAll
add_test: should not run
finish_describe_definition: test suite beforeAll
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: test suite beforeAll
hook_start: beforeAll
beforeAll 1 runs
hook_failure: beforeAll
hook_start: beforeAll
beforeAll 2 runs
hook_failure: beforeAll
test_start: should not run
test_fn_start: should not run
test_done: should not run
run_describe_finish: test suite beforeAll
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish

unhandledErrors: 0",
}
`;

envinfo


njmaeff avatar Nov 03 '20 01:11 njmaeff

Imo if beforeAll hook fail it should skip all related tests and afterAll hook should be run in any case (to ensure clean up is done, because beforeAll hook may initialize something partially)

fider-apptimia avatar Nov 04 '20 11:11 fider-apptimia

@fider-apptimia The current behaviour for jest circus is to mark the test as error when a beforeAll hook fails. The test will not run but the rest of the beforeAll, afterEach, and afterAll hooks will still run. I feel like once the first beforeAll fails, the rest of the beforeAll hooks should skip and then afterAll hooks immediately run. There is an open issue to fix this in the jest jasmine runner. I didn't see any issue specifically related to circus for this behaviour.

njmaeff avatar Nov 04 '20 12:11 njmaeff

@njmaeff imo issue here is that tests will be executed even if beforeAll throw.

fider-apptimia avatar Nov 04 '20 14:11 fider-apptimia

Tests should definitely not run if before* fails. Whether or not after* should run is probably a separate discussion

SimenB avatar Nov 04 '20 15:11 SimenB

afterEach() is also run in case beforeAll() fails. Is that a separate issue?

thernstig avatar Mar 18 '21 17:03 thernstig

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.

github-actions[bot] avatar Feb 17 '23 16:02 github-actions[bot]

Stale? Has this been fixed. I think this is still an issue and should be fixed. I think @SimenB's comment summarized it perfectly:

Tests should definitely not run if before* fails. Whether or not after* should run is probably a separate discussion

joshuapinter avatar Feb 17 '23 16:02 joshuapinter

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.

github-actions[bot] avatar Feb 17 '24 17:02 github-actions[bot]

Still not fixed as far as I'm aware of so we should keep it open.

joshuapinter avatar Feb 18 '24 01:02 joshuapinter