Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix beforeEach hook execution order #6673

Merged
merged 1 commit into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before the fix this would print before each 2 and the next one (line 104) would be before each 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these snapshots, really easy to review

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