diff --git a/integration-tests/__tests__/__snapshots__/empty-describe-with-hooks.test.js.snap b/integration-tests/__tests__/__snapshots__/empty-describe-with-hooks.test.js.snap index c308e8d5f692..500475fed574 100644 --- a/integration-tests/__tests__/__snapshots__/empty-describe-with-hooks.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/empty-describe-with-hooks.test.js.snap @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/packages/jest-circus/src/index.js b/packages/jest-circus/src/index.js index d933476ea460..0cc9e98e82ce 100644 --- a/packages/jest-circus/src/index.js +++ b/packages/jest-circus/src/index.js @@ -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') {