Skip to content

Commit

Permalink
fix beforeAll hooks in jest-circus (#6681)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov authored Jul 11, 2018
1 parent 40b0c6a commit 7d9544b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ finish_describe_definition: describe
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
hook_start: beforeAll
> beforeAll
hook_success: beforeAll
run_describe_start: child describe
test_start: my test
hook_start: beforeEach
> beforeEach
hook_success: beforeEach
test_fn_start: my test
test_fn_failure: my test
> my test
test_fn_success: my test
hook_start: afterEach
> afterEach
hook_success: afterEach
test_done: my test
run_describe_finish: child describe
hook_start: afterAll
> afterAll
hook_success: afterAll
run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`beforeAll is exectued correctly 1`] = `
"start_describe_definition: describe 1
add_hook: beforeAll
add_test: test 1
start_describe_definition: 2nd level describe
add_hook: beforeAll
add_test: test 2
add_test: test 3
finish_describe_definition: 2nd level describe
finish_describe_definition: describe 1
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe 1
hook_start: beforeAll
> beforeAll 1
hook_success: beforeAll
test_start: test 1
test_fn_start: test 1
> test 1
test_fn_success: test 1
test_done: test 1
run_describe_start: 2nd level describe
hook_start: beforeAll
> beforeAll 2
hook_success: beforeAll
test_start: test 2
test_fn_start: test 2
> test 2
test_fn_success: test 2
test_done: test 2
test_start: test 3
test_fn_start: test 3
> test 3
test_fn_success: test 3
test_done: test 3
run_describe_finish: 2nd level describe
run_describe_finish: describe 1
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0"
`;

exports[`beforeEach is executed before each test in current/child describe blocks 1`] = `
"start_describe_definition: describe
add_hook: beforeEach
Expand Down
12 changes: 5 additions & 7 deletions packages/jest-circus/src/__tests__/after_all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ test('describe block cannot have hooks and no tests', () => {
test('describe block _can_ have hooks if a child describe block has tests', () => {
const result = runTest(`
describe('describe', () => {
afterEach(() => {});
beforeEach(() => {});
afterAll(() => {});
beforeAll(() => {});
afterEach(() => console.log('> afterEach'));
beforeEach(() => console.log('> beforeEach'));
afterAll(() => console.log('> afterAll'));
beforeAll(() => console.log('> beforeAll'));
describe('child describe', () => {
test('my test', () => {
expect(true).toBe(true);
})
test('my test', () => console.log('> my test'));
})
})
`);
Expand Down
17 changes: 17 additions & 0 deletions packages/jest-circus/src/__tests__/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ test('multiple before each hooks in one describe are executed in the right order

expect(stdout).toMatchSnapshot();
});

test('beforeAll is exectued correctly', () => {
const {stdout} = runTest(`
describe('describe 1', () => {
beforeAll(() => console.log('> beforeAll 1'));
test('test 1', () => console.log('> test 1'));
describe('2nd level describe', () => {
beforeAll(() => console.log('> beforeAll 2'));
test('test 2', () => console.log('> test 2'));
test('test 3', () => console.log('> test 3'));
});
});
`);

expect(stdout).toMatchSnapshot();
});
6 changes: 5 additions & 1 deletion packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,20 @@ export const makeTest = (
};
};

// Traverse the tree of describe blocks and return true if at least one describe
// block has an enabled test.
const hasEnabledTest = (describeBlock: DescribeBlock): boolean => {
const {hasFocusedTests, testNamePattern} = getState();
return describeBlock.tests.some(
const hasOwnEnabledTests = describeBlock.tests.some(
test =>
!(
test.mode === 'skip' ||
(hasFocusedTests && test.mode !== 'only') ||
(testNamePattern && !testNamePattern.test(getTestID(test)))
),
);

return hasOwnEnabledTests || describeBlock.children.some(hasEnabledTest);
};

export const getAllHooksForDescribe = (
Expand Down

0 comments on commit 7d9544b

Please sign in to comment.