From eb111997f2ae866f4fd3864f0a8a3974f1b48ecc Mon Sep 17 00:00:00 2001 From: Aaron Abramov Date: Tue, 10 Jul 2018 19:10:27 -0700 Subject: [PATCH] fix beforeEach hook execution order --- .../__snapshots__/hooks.test.js.snap | 39 +++++++++++++++++++ .../jest-circus/src/__tests__/hooks.test.js | 28 +++++++++++-- packages/jest-circus/src/utils.js | 8 ++-- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.js.snap b/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.js.snap index f7deba86c216..1bef73d5a221 100644 --- a/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.js.snap +++ b/packages/jest-circus/src/__tests__/__snapshots__/hooks.test.js.snap @@ -23,12 +23,14 @@ run_describe_start: ROOT_DESCRIBE_BLOCK run_describe_start: describe test_start: one hook_start: beforeEach +> describe beforeEach hook_success: beforeEach test_fn_start: one test_fn_success: one test_done: one test_start: two hook_start: beforeEach +> describe beforeEach hook_success: beforeEach test_fn_start: two test_fn_success: two @@ -36,8 +38,10 @@ test_done: two run_describe_start: 2nd level describe test_start: 2nd level test hook_start: beforeEach +> describe beforeEach hook_success: beforeEach hook_start: beforeEach +> 2nd level describe beforeEach hook_success: beforeEach test_fn_start: 2nd level test test_fn_success: 2nd level test @@ -45,16 +49,20 @@ test_done: 2nd level test run_describe_start: 3rd level describe test_start: 3rd level test hook_start: beforeEach +> describe beforeEach hook_success: beforeEach hook_start: beforeEach +> 2nd level describe beforeEach hook_success: beforeEach test_fn_start: 3rd level test test_fn_success: 3rd level test test_done: 3rd level test test_start: 3rd level test#2 hook_start: beforeEach +> describe beforeEach hook_success: beforeEach hook_start: beforeEach +> 2nd level describe beforeEach hook_success: beforeEach test_fn_start: 3rd level test#2 test_fn_success: 3rd level test#2 @@ -65,6 +73,7 @@ run_describe_finish: describe run_describe_start: 2nd describe test_start: 2nd describe test hook_start: beforeEach +> 2nd describe beforeEach that throws hook_failure: beforeEach test_fn_start: 2nd describe test test_done: 2nd describe test @@ -74,3 +83,33 @@ run_finish unhandledErrors: 0" `; + +exports[`multiple before each hooks in one describe are executed in the right order 1`] = ` +"start_describe_definition: describe 1 +add_hook: beforeEach +add_hook: beforeEach +start_describe_definition: 2nd level describe +add_test: test +finish_describe_definition: 2nd level describe +finish_describe_definition: describe 1 +run_start +run_describe_start: ROOT_DESCRIBE_BLOCK +run_describe_start: describe 1 +run_describe_start: 2nd level describe +test_start: test +hook_start: beforeEach +before each 1 +hook_success: beforeEach +hook_start: beforeEach +before each 2 +hook_success: beforeEach +test_fn_start: test +test_fn_success: test +test_done: test +run_describe_finish: 2nd level describe +run_describe_finish: describe 1 +run_describe_finish: ROOT_DESCRIBE_BLOCK +run_finish + +unhandledErrors: 0" +`; diff --git a/packages/jest-circus/src/__tests__/hooks.test.js b/packages/jest-circus/src/__tests__/hooks.test.js index c9509bd2ea4e..94c482118e1a 100644 --- a/packages/jest-circus/src/__tests__/hooks.test.js +++ b/packages/jest-circus/src/__tests__/hooks.test.js @@ -15,11 +15,11 @@ import {runTest} from '../__mocks__/test_utils'; test('beforeEach is executed before each test in current/child describe blocks', () => { const {stdout} = runTest(` describe('describe', () => { - beforeEach(() => {}); + beforeEach(() => console.log('> describe beforeEach')); test('one', () => {}); test('two', () => {}); describe('2nd level describe', () => { - beforeEach(() => {}); + beforeEach(() => console.log('> 2nd level describe beforeEach')); test('2nd level test', () => {}); describe('3rd level describe', () => { @@ -30,10 +30,32 @@ test('beforeEach is executed before each test in current/child describe blocks', }) describe('2nd describe', () => { - beforeEach(() => { throw new Error('alabama'); }); + beforeEach(() => { + console.log('> 2nd describe beforeEach that throws') + throw new Error('alabama'); + }); test('2nd describe test', () => {}); }) `); expect(stdout).toMatchSnapshot(); }); + +test('multiple before each hooks in one describe are executed in the right order', () => { + const {stdout} = runTest(` + describe('describe 1', () => { + beforeEach(() => { + console.log('before each 1'); + }); + beforeEach(() => { + console.log('before each 2'); + }); + + describe('2nd level describe', () => { + test('test', () => {}); + }); + }); + `); + + expect(stdout).toMatchSnapshot(); +}); diff --git a/packages/jest-circus/src/utils.js b/packages/jest-circus/src/utils.js index eda159eb7d74..5a606026cdac 100644 --- a/packages/jest-circus/src/utils.js +++ b/packages/jest-circus/src/utils.js @@ -130,18 +130,20 @@ export const getEachHooksForTest = ( let {parent: block} = test; do { + const beforeEachForCurrentBlock = []; for (const hook of block.hooks) { switch (hook.type) { case 'beforeEach': - // Before hooks are executed from top to bottom, the opposite of the - // way we traversed it. - result.beforeEach.unshift(hook); + beforeEachForCurrentBlock.push(hook); break; case 'afterEach': result.afterEach.push(hook); break; } } + // 'beforeEach' hooks are executed from top to bottom, the opposite of the + // way we traversed it. + result.beforeEach = [...beforeEachForCurrentBlock, ...result.beforeEach]; } while ((block = block.parent)); return result; };