Skip to content

Commit

Permalink
fix beforeEach hook execution order (#6673)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov authored Jul 11, 2018
1 parent c32d2d3 commit 40b0c6a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,46 @@ 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
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
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
Expand All @@ -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
Expand All @@ -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"
`;
28 changes: 25 additions & 3 deletions packages/jest-circus/src/__tests__/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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();
});
8 changes: 5 additions & 3 deletions packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit 40b0c6a

Please sign in to comment.