MCPcopy Create free account
hub / github.com/angular/angular / patchJasmine

Function patchJasmine

packages/zone.js/lib/jasmine/jasmine.ts:16–353  ·  view source on GitHub ↗
(Zone: ZoneType)

Source from the content-addressed store, hash-verified

14declare let jest: any;
15
16export function patchJasmine(Zone: ZoneType): void {
17 Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
18 const __extends = function (d: any, b: any) {
19 for (const p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
20 function __(this: Object) {
21 this.constructor = d;
22 }
23 d.prototype =
24 b === null ? Object.create(b) : ((__.prototype = b.prototype), new (__ as any)());
25 };
26 // Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs
27 // in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503)
28 if (!Zone) throw new Error('Missing: zone.js');
29 if (typeof jest !== 'undefined') {
30 // return if jasmine is a light implementation inside jest
31 // in this case, we are running inside jest not jasmine
32 return;
33 }
34 if (typeof jasmine == 'undefined' || (jasmine as any)['__zone_patch__']) {
35 return;
36 }
37 (jasmine as any)['__zone_patch__'] = true;
38
39 const SyncTestZoneSpec: {new (name: string): ZoneSpec} = (Zone as any)['SyncTestZoneSpec'];
40 const ProxyZoneSpec: {new (): ZoneSpec} = (Zone as any)['ProxyZoneSpec'];
41 if (!SyncTestZoneSpec) throw new Error('Missing: SyncTestZoneSpec');
42 if (!ProxyZoneSpec) throw new Error('Missing: ProxyZoneSpec');
43
44 const ambientZone = Zone.current;
45
46 const symbol = Zone.__symbol__;
47
48 // whether patch jasmine clock when in fakeAsync
49 const disablePatchingJasmineClock = global[symbol('fakeAsyncDisablePatchingClock')] === true;
50 // the original variable name fakeAsyncPatchLock is not accurate, so the name will be
51 // fakeAsyncAutoFakeAsyncWhenClockPatched and if this enablePatchingJasmineClock is false, we
52 // also automatically disable the auto jump into fakeAsync feature
53 const enableAutoFakeAsyncWhenClockPatched =
54 !disablePatchingJasmineClock &&
55 (global[symbol('fakeAsyncPatchLock')] === true ||
56 global[symbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] === true);
57
58 // Monkey patch all of the jasmine DSL so that each function runs in appropriate zone.
59 const jasmineEnv: any = jasmine.getEnv();
60 ['describe', 'xdescribe', 'fdescribe'].forEach((methodName) => {
61 let originalJasmineFn: Function = jasmineEnv[methodName];
62 jasmineEnv[methodName] = function (description: string, specDefinitions: Function) {
63 return originalJasmineFn.call(
64 this,
65 description,
66 wrapDescribeInZone(description, specDefinitions),
67 );
68 };
69 });
70 ['it', 'xit', 'fit'].forEach((methodName) => {
71 let originalJasmineFn: Function = jasmineEnv[methodName];
72 jasmineEnv[symbol(methodName)] = originalJasmineFn;
73 jasmineEnv[methodName] = function (

Callers 3

rollup-jasmine.tsFile · 0.90
rollupTestingFunction · 0.90

Calls 10

__extendsFunction · 0.85
__symbol__Method · 0.80
wrapDescribeInZoneFunction · 0.70
wrapTestInZoneFunction · 0.70
__load_patchMethod · 0.65
applyMethod · 0.65
getMethod · 0.65
forkMethod · 0.65
scheduleMicroTaskMethod · 0.65
forEachMethod · 0.45

Tested by

no test coverage detected