From 85fe9425205b08033bb9343a28804c9a75d9ecc0 Mon Sep 17 00:00:00 2001 From: Cameron Little Date: Sun, 16 Aug 2020 12:16:05 +0200 Subject: [PATCH] Deduplicate Jasmine global test location wrappers --- packages/jest-jasmine2/src/index.ts | 50 +++++++++++------------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 7c081b7846b1..fcb593f40609 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -47,38 +47,26 @@ async function jasmine2( // TODO: Remove config option if V8 exposes some way of getting location of caller // in a future version if (config.testLocationInResults === true) { - const originalIt = environment.global.it; - environment.global.it = ((...args) => { - const stack = getCallsite(1, runtime.getSourceMaps()); - const it = originalIt(...args); - - // @ts-expect-error - it.result.__callsite = stack; - - return it; - }) as Global.Global['it']; - - const originalXit = environment.global.xit; - environment.global.xit = ((...args) => { - const stack = getCallsite(1, runtime.getSourceMaps()); - const xit = originalXit(...args); - - // @ts-expect-error - xit.result.__callsite = stack; - - return xit; - }) as Global.Global['xit']; - - const originalFit = environment.global.fit; - environment.global.fit = ((...args) => { - const stack = getCallsite(1, runtime.getSourceMaps()); - const fit = originalFit(...args); - - // @ts-expect-error - fit.result.__callsite = stack; + function wrapIt(original: T): T { + const wrapped = ( + testName: Global.TestName, + fn: Global.TestFn, + timeout?: number, + ) => { + const stack = getCallsite(1, runtime.getSourceMaps()); + const it = original(testName, fn, timeout); + + // @ts-expect-error + it.result.__callsite = stack; + + return it; + }; + return (wrapped as any) as T; + } - return fit; - }) as Global.Global['fit']; + environment.global.it = wrapIt(environment.global.it); + environment.global.xit = wrapIt(environment.global.xit); + environment.global.fit = wrapIt(environment.global.fit); } jasmineAsyncInstall(globalConfig, environment.global);