Skip to content

Commit

Permalink
afterAll hook fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed Jun 2, 2017
1 parent 657f967 commit 7afd7e1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
28 changes: 19 additions & 9 deletions packages/jest-framework/src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import type {
const {getState, dispatch} = require('./state');
const {
callAsyncFn,
getAllHooks,
getAllHooksForDescribe,
getEachHooksForTest,
isSharedHook,
makeTestResults,
} = require('./utils');
Expand All @@ -34,31 +35,40 @@ const run = async (): Promise<TestResults> => {

const _runTestsForDescribeBlock = async (describeBlock: DescribeBlock) => {
dispatch({describeBlock, name: 'run_describe_start'});
const {beforeAll, afterAll} = getAllHooksForDescribe(describeBlock);

for (const hook of beforeAll) {
_callHook(hook);
}
for (const test of describeBlock.tests) {
await _runTest(test);
}
for (const child of describeBlock.children) {
await _runTestsForDescribeBlock(child);
}

for (const hook of afterAll) {
_callHook(hook);
}
dispatch({describeBlock, name: 'run_describe_finish'});
};

const _runTest = async (test: Test): Promise<void> => {
const testContext = Object.create(null);
const {afterHooks, beforeHooks} = getAllHooks(test);
const {afterEach, beforeEach} = getEachHooksForTest(test);

for (const hook of beforeHooks) {
await callHook(hook, testContext);
for (const hook of beforeEach) {
await _callHook(hook, testContext);
}

await callTest(test, testContext);
await _callTest(test, testContext);

for (const hook of afterHooks) {
await callHook(hook, testContext);
for (const hook of afterEach) {
await _callHook(hook, testContext);
}
};

const callHook = (hook: Hook, testContext: TestContext): Promise<any> => {
const _callHook = (hook: Hook, testContext?: TestContext): Promise<any> => {
const {sharedHooksThatHaveBeenExecuted} = getState();
if (isSharedHook(hook) && sharedHooksThatHaveBeenExecuted.has(hook)) {
return Promise.resolve();
Expand All @@ -70,7 +80,7 @@ const callHook = (hook: Hook, testContext: TestContext): Promise<any> => {
.catch(error => dispatch({error, hook, name: 'hook_failure'}));
};

const callTest = (test: Test, testContext: TestContext): Promise<any> => {
const _callTest = (test: Test, testContext: TestContext): Promise<any> => {
const isSkipped =
test.mode === 'skip' ||
(getState().hasFocusedTests && test.mode !== 'only');
Expand Down
43 changes: 29 additions & 14 deletions packages/jest-framework/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,45 @@ const makeTest = (
};
};

const getAllHooks = (
const getAllHooksForDescribe = (
describe: DescribeBlock,
): {[key: 'beforeAll' | 'afterAll']: Array<Hook>} => {
const result = {afterAll: [], beforeAll: []};

for (const hook of describe.hooks) {
switch (hook.type) {
case 'beforeAll':
result.beforeAll.push(hook);
break;
case 'afterAll':
result.afterAll.push(hook);
break;
}
}

return result;
};

const getEachHooksForTest = (
test: Test,
): {[key: 'beforeHooks' | 'afterHooks']: Array<Hook>} => {
const result = {afterHooks: [], beforeHooks: []};
): {[key: 'beforeEach' | 'afterEach']: Array<Hook>} => {
const result = {afterEach: [], beforeEach: []};
let {parent: block} = test;

do {
for (const hook of block.hooks) {
switch (hook.type) {
case 'beforeEach':
case 'beforeAll':
result.beforeHooks.push(hook);
result.beforeEach.push(hook);
break;
case 'afterEach':
case 'afterAll':
result.afterHooks.push(hook);
// Before hooks are executed from top to bottom, the opposite of the
// way we traversed it.
result.afterEach.unshift(hook);
break;
default:
throw new Error(`unexpected hook type: ${hook.type}`);
}
}
} while ((block = block.parent));
// Before hooks are executed from top to bottom, the opposite of the way
// we traversed it.
result.beforeHooks.reverse();
return result;
};

Expand All @@ -106,7 +120,7 @@ const isSharedHook = (hook: Hook): boolean => SHARED_HOOK_TYPES.has(hook.type);

const callAsyncFn = (
fn: AsyncFn,
testContext: TestContext,
testContext: ?TestContext,
{isHook}: {isHook?: boolean} = {isHook: false},
): Promise<any> => {
// If this fn accepts `done` callback we return a promise that fullfills as
Expand Down Expand Up @@ -208,7 +222,8 @@ const _formatError = (error: ?Exception): string => {

module.exports = {
callAsyncFn,
getAllHooks,
getAllHooksForDescribe,
getEachHooksForTest,
getTestDuration,
getTestID,
isSharedHook,
Expand Down

0 comments on commit 7afd7e1

Please sign in to comment.