forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make test suite exit early if beforeEach hook fails (closes jest…
- Loading branch information
1 parent
2b64bb4
commit 64867c7
Showing
14 changed files
with
587 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 267 additions & 0 deletions
267
e2e/__tests__/__snapshots__/beforeEachAbortsTests.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 beforeEach › nested block › first 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 beforeEach › nested block › second 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 beforeEach › nested block › 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('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 beforeEach › nested block › 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('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 tests › skipped 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 tests › skipped 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 hook › skipped 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 hooks › skipped 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 test › skipped 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 hooks › skipped 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 hooks › skipped 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.", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
e2e/before-each-aborts-tests/__tests__/nestedBlocks/afterAllHook.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Oops, something went wrong.