Skip to content

Commit

Permalink
Deduplicate Jasmine global test location wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
apexskier committed Aug 16, 2020
1 parent 6cdff68 commit 8100b47
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions packages/jest-jasmine2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {SnapshotStateType} from 'jest-snapshot';
import type {RuntimeType as Runtime} from 'jest-runtime';

import {getCallsite} from '@jest/source-map';
import {unlink} from 'graceful-fs';
import installEach from './each';
import {installErrorOnPrivate} from './errorOnPrivate';
import JasmineReporter from './reporter';
Expand Down Expand Up @@ -47,38 +48,29 @@ 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);
function wrapIt(original: (...args: Array<unknown>) => unknown) {
return (...args: Array<unknown>) => {
const stack = getCallsite(1, runtime.getSourceMaps());
const it = original(...args);

// @ts-expect-error
it.result.__callsite = stack;
// @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'];
return it;
};
}

const originalFit = environment.global.fit;
environment.global.fit = ((...args) => {
const stack = getCallsite(1, runtime.getSourceMaps());
const fit = originalFit(...args);
environment.global.it = wrapIt(
environment.global.it,
) as Global.Global['it'];

// @ts-expect-error
fit.result.__callsite = stack;
environment.global.it = wrapIt(
environment.global.xit,
) as Global.Global['xit'];

return fit;
}) as Global.Global['fit'];
environment.global.fit = wrapIt(
environment.global.fit,
) as Global.Global['fit'];
}

jasmineAsyncInstall(globalConfig, environment.global);
Expand Down

0 comments on commit 8100b47

Please sign in to comment.