Skip to content

Commit

Permalink
Trim stack trace for hooks in empty describe blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed May 27, 2018
1 parent af0776c commit b689d1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Object {
11 |
12 | describe('another block with tests', () => {
at packages/jest-circus/build/index.js:33:17
at __tests__/hook-in-empty-describe.test.js:9:3
at __tests__/hook-in-empty-describe.test.js:8:1
Expand Down Expand Up @@ -48,7 +47,6 @@ Object {
11 | });
12 |
at packages/jest-circus/build/index.js:33:17
at __tests__/hook-in-empty-nested-describe.test.js:9:3
at __tests__/hook-in-empty-nested-describe.test.js:8:1
Expand Down Expand Up @@ -79,7 +77,6 @@ Object {
11 | afterAll(() => {});
12 | beforeAll(() => {});
at packages/jest-circus/build/index.js:33:17
at __tests__/multiple-hooks-in-empty-describe.test.js:9:3
at __tests__/multiple-hooks-in-empty-describe.test.js:8:1
Expand All @@ -95,7 +92,6 @@ Object {
12 | beforeAll(() => {});
13 | });
at packages/jest-circus/build/index.js:33:17
at __tests__/multiple-hooks-in-empty-describe.test.js:10:3
at __tests__/multiple-hooks-in-empty-describe.test.js:8:1
Expand All @@ -111,7 +107,6 @@ Object {
13 | });
14 |
at packages/jest-circus/build/index.js:33:17
at __tests__/multiple-hooks-in-empty-describe.test.js:11:3
at __tests__/multiple-hooks-in-empty-describe.test.js:8:1
Expand All @@ -127,7 +122,6 @@ Object {
14 |
15 | describe('another block with tests', () => {
at packages/jest-circus/build/index.js:33:17
at __tests__/multiple-hooks-in-empty-describe.test.js:12:3
at __tests__/multiple-hooks-in-empty-describe.test.js:8:1
Expand Down
23 changes: 17 additions & 6 deletions packages/jest-circus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,23 @@ const _dispatchDescribe = (blockFn, blockName, mode?: BlockMode) => {
dispatch({blockName, mode, name: 'finish_describe_definition'});
};

const _addHook = (fn: HookFn, hookType: HookType, timeout: ?number) =>
dispatch({asyncError: new Error(), fn, hookType, name: 'add_hook', timeout});
const beforeEach: THook = (fn, timeout) => _addHook(fn, 'beforeEach', timeout);
const beforeAll: THook = (fn, timeout) => _addHook(fn, 'beforeAll', timeout);
const afterEach: THook = (fn, timeout) => _addHook(fn, 'afterEach', timeout);
const afterAll: THook = (fn, timeout) => _addHook(fn, 'afterAll', timeout);
const _addHook = (fn: HookFn, hookType: HookType, hookFn, timeout: ?number) => {
const asyncError = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, hookFn);
}
dispatch({asyncError, fn, hookType, name: 'add_hook', timeout});
};

// Hooks have to pass themselves to the HOF in order for us to trim stack traces.
const beforeEach: THook = (fn, timeout) =>
_addHook(fn, 'beforeEach', beforeEach, timeout);
const beforeAll: THook = (fn, timeout) =>
_addHook(fn, 'beforeAll', beforeAll, timeout);
const afterEach: THook = (fn, timeout) =>
_addHook(fn, 'afterEach', afterEach, timeout);
const afterAll: THook = (fn, timeout) =>
_addHook(fn, 'afterAll', afterAll, timeout);

const test = (testName: TestName, fn: TestFn, timeout?: number) => {
if (typeof testName !== 'string') {
Expand Down

0 comments on commit b689d1f

Please sign in to comment.