Skip to content

Commit

Permalink
fix: make test suite exit early if beforeEach hook fails (closes jest…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed Jul 7, 2019
1 parent 2b64bb4 commit 64867c7
Show file tree
Hide file tree
Showing 14 changed files with 587 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- `[jest-snapshot]` Prevent inline snapshots from drifting when inline snapshots are updated ([#8492](https://github.com/facebook/jest/pull/8492))
- `[jest-haste-map]` Don't throw on missing mapper in Node crawler ([#8558](https://github.com/facebook/jest/pull/8558))
- `[jest-core]` Fix incorrect `passWithNoTests` warning ([#8595](https://github.com/facebook/jest/pull/8595))
- `[jest-jasmine2]` Make test suite exit early if a beforeEach hook fails ([#8651](https://github.com/facebook/jest/pull/8651))

### Chore & Maintenance

Expand Down
267 changes: 267 additions & 0 deletions e2e/__tests__/__snapshots__/beforeEachAbortsTests.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`inside deeply nested blocks can cancel tests only for the nested describe block it is in 1`] = `
Object {
"rest": "FAIL __tests__/nestedBlocks/testsInDifferentBlocks.test.js
tests for the nested beforeEach
outer test
nested block
first nested test
second nested test
tests for the nested beforeEachnested blockfirst nested test
The nested beforeEach hook failed.
14 | beforeEach(() => {
15 | console.log('nested beforeEach');
> 16 | throw new Error('The nested beforeEach hook failed.');
| ^
17 | });
18 |
19 | it('first nested test', () => {
at Object.beforeEach (__tests__/nestedBlocks/testsInDifferentBlocks.test.js:16:13)
tests for the nested beforeEachnested blocksecond nested test
The nested beforeEach hook failed.
14 | beforeEach(() => {
15 | console.log('nested beforeEach');
> 16 | throw new Error('The nested beforeEach hook failed.');
| ^
17 | });
18 |
19 | it('first nested test', () => {
at Object.beforeEach (__tests__/nestedBlocks/testsInDifferentBlocks.test.js:16:13)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 2 failed, 1 passed, 3 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /nestedBlocks\\\\/testsInDifferentBlocks.test.js/i.",
}
`;
exports[`inside deeply nested blocks does not run the afterAll hooks if a nested beforeEach fails 1`] = `
Object {
"rest": "FAIL __tests__/nestedBlocks/afterAllHook.test.js
afterAll hooks with a nested beforeEach
outer test
nested block
nested test
afterAll hooks with a nested beforeEachnested blocknested test
The nested beforeEach hook failed.
14 | beforeEach(() => {
15 | console.log('nested beforeEach');
> 16 | throw new Error('The nested beforeEach hook failed.');
| ^
17 | });
18 |
19 | it('nested test', () => {
at Object.beforeEach (__tests__/nestedBlocks/afterAllHook.test.js:16:13)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /nestedBlocks\\\\/afterAllHook.test.js/i.",
}
`;
exports[`inside deeply nested blocks does not run the afterEach hooks for the test whose beforeEach failed 1`] = `
Object {
"rest": "FAIL __tests__/nestedBlocks/afterEachHook.test.js
afterEach hooks with a nested beforeEach
outer test
nested block
nested test
afterEach hooks with a nested beforeEachnested blocknested test
The nested beforeEach hook failed.
14 | beforeEach(() => {
15 | console.log('nested beforeEach');
> 16 | throw new Error('The nested beforeEach hook failed.');
| ^
17 | });
18 |
19 | it('nested test', () => {
at Object.beforeEach (__tests__/nestedBlocks/afterEachHook.test.js:16:13)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /nestedBlocks\\\\/afterEachHook.test.js/i.",
}
`;
exports[`without nested blocks does not run any of the tests if beforeEach fails 1`] = `
Object {
"rest": "FAIL __tests__/singleBlock/multipleTests.test.js
a block with multiple tests
skipped first test
skipped second test
a block with multiple testsskipped first test
The beforeEach hook failed.
9 | beforeEach(() => {
10 | console.log('beforeEach');
> 11 | throw new Error('The beforeEach hook failed.');
| ^
12 | });
13 |
14 | test('skipped first test', () => {
at Object.beforeEach (__tests__/singleBlock/multipleTests.test.js:11:11)
a block with multiple testsskipped second test
The beforeEach hook failed.
9 | beforeEach(() => {
10 | console.log('beforeEach');
> 11 | throw new Error('The beforeEach hook failed.');
| ^
12 | });
13 |
14 | test('skipped first test', () => {
at Object.beforeEach (__tests__/singleBlock/multipleTests.test.js:11:11)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /singleBlock\\\\/multipleTests.test.js/i.",
}
`;
exports[`without nested blocks does not run the afterAll hook if the beforeEach hook fails 1`] = `
Object {
"rest": "FAIL __tests__/singleBlock/afterAllHook.test.js
a block with an afterAll hook
skipped test
a block with an afterAll hookskipped test
The beforeEach hook failed.
9 | beforeEach(() => {
10 | console.log('beforeEach');
> 11 | throw new Error('The beforeEach hook failed.');
| ^
12 | });
13 |
14 | test('skipped test', () => {
at Object.beforeEach (__tests__/singleBlock/afterAllHook.test.js:11:11)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /singleBlock\\\\/afterAllHook.test.js/i.",
}
`;
exports[`without nested blocks does not run the afterEach hook if the beforeEach hook fails 1`] = `
Object {
"rest": "FAIL __tests__/singleBlock/afterEachHook.test.js
a block with afterEach hooks
skipped test
a block with afterEach hooksskipped test
The beforeEach hook failed.
9 | beforeEach(() => {
10 | console.log('beforeEach');
> 11 | throw new Error('The beforeEach hook failed.');
| ^
12 | });
13 |
14 | test('skipped test', () => {
at Object.beforeEach (__tests__/singleBlock/afterEachHook.test.js:11:11)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /singleBlock\\\\/afterEachHook.test.js/i.",
}
`;
exports[`without nested blocks does not run the test if beforeEach fails 1`] = `
Object {
"rest": "FAIL __tests__/singleBlock/singleTest.test.js
a block with a single test
skipped test
a block with a single testskipped test
The beforeEach hook failed.
9 | beforeEach(() => {
10 | console.log('beforeEach');
> 11 | throw new Error('The beforeEach hook failed.');
| ^
12 | });
13 |
14 | test('skipped test', () => {
at Object.beforeEach (__tests__/singleBlock/singleTest.test.js:11:11)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /singleBlock\\\\/singleTest.test.js/i.",
}
`;
exports[`without nested blocks runs all of the beforeEach hooks if one fails but does not run the tests 1`] = `
Object {
"rest": "FAIL __tests__/singleBlock/multipleBeforeEachHooks.test.js
a block with multiple beforeEach hooks
skipped test
a block with multiple beforeEach hooksskipped test
The first beforeEach hook failed.
9 | beforeEach(() => {
10 | console.log('first beforeEach');
> 11 | throw new Error('The first beforeEach hook failed.');
| ^
12 | });
13 |
14 | beforeEach(() => {
at Object.beforeEach (__tests__/singleBlock/multipleBeforeEachHooks.test.js:11:11)
a block with multiple beforeEach hooksskipped test
The second beforeEach hook failed.
14 | beforeEach(() => {
15 | console.log('second beforeEach');
> 16 | throw new Error('The second beforeEach hook failed.');
| ^
17 | });
18 |
19 | test('skipped test', () => {
at Object.beforeEach (__tests__/singleBlock/multipleBeforeEachHooks.test.js:16:11)",
"summary": "Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /singleBlock\\\\/multipleBeforeEachHooks.test.js/i.",
}
`;
68 changes: 68 additions & 0 deletions e2e/__tests__/beforeEachAbortsTests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
import runJest from '../runJest';
import {extractSummary} from '../Utils';

const dir = path.resolve(__dirname, '../before-each-aborts-tests');

describe('without nested blocks', () => {
test('does not run the test if beforeEach fails', () => {
const result = runJest(dir, ['singleBlock/singleTest.test.js']);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});

test('does not run any of the tests if beforeEach fails', () => {
const result = runJest(dir, ['singleBlock/multipleTests.test.js']);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});

test('runs all of the beforeEach hooks if one fails but does not run the tests', () => {
const result = runJest(dir, [
'singleBlock/multipleBeforeEachHooks.test.js',
]);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});

test('does not run the afterEach hook if the beforeEach hook fails', () => {
const result = runJest(dir, ['singleBlock/afterEachHook.test.js']);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});

test('does not run the afterAll hook if the beforeEach hook fails', () => {
const result = runJest(dir, ['singleBlock/afterAllHook.test.js']);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});
});

describe('inside deeply nested blocks', () => {
test('can cancel tests only for the nested describe block it is in', () => {
const result = runJest(dir, [
'nestedBlocks/testsInDifferentBlocks.test.js',
]);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});

test('does not run the afterAll hooks if a nested beforeEach fails', () => {
const result = runJest(dir, ['nestedBlocks/afterAllHook.test.js']);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});

test('does not run the afterEach hooks for the test whose beforeEach failed', () => {
const result = runJest(dir, ['nestedBlocks/afterEachHook.test.js']);
expect(result.status).toBe(1);
expect(extractSummary(result.stderr)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

describe('afterAll hooks with a nested beforeEach', () => {
it('outer test', () => {
console.log('outer test');
});

describe('nested block', () => {
beforeEach(() => {
console.log('nested beforeEach');
throw new Error('The nested beforeEach hook failed.');
});

it('nested test', () => {
console.log('nested test');
});

afterAll(() => {
console.log('nested afterAll');
});
});

afterAll(() => {
console.log('outer afterAll');
});
});
Loading

0 comments on commit 64867c7

Please sign in to comment.