feat: make jasmine patch as zone module (#34676)

PR Close #34676
This commit is contained in:
JiaLiPassion 2020-01-08 23:05:27 +09:00 committed by Kara Erickson
parent 5f57376899
commit e1160f19be
1 changed files with 14 additions and 13 deletions

View File

@ -10,7 +10,7 @@
'use strict'; 'use strict';
declare let jest: any; declare let jest: any;
((_global: any) => { Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
const __extends = function(d: any, b: any) { const __extends = function(d: any, b: any) {
for (const p in b) for (const p in b)
if (b.hasOwnProperty(p)) d[p] = b[p]; if (b.hasOwnProperty(p)) d[p] = b[p];
@ -25,9 +25,9 @@ declare let jest: any;
// in this case, we are running inside jest not jasmine // in this case, we are running inside jest not jasmine
return; return;
} }
if (typeof jasmine == 'undefined') throw new Error('Missing: jasmine.js'); if (typeof jasmine == 'undefined' || (jasmine as any)['__zone_patch__']) {
if ((jasmine as any)['__zone_patch__']) return;
throw new Error(`'jasmine' has already been patched with 'Zone'.`); }
(jasmine as any)['__zone_patch__'] = true; (jasmine as any)['__zone_patch__'] = true;
const SyncTestZoneSpec: {new (name: string): ZoneSpec} = (Zone as any)['SyncTestZoneSpec']; const SyncTestZoneSpec: {new (name: string): ZoneSpec} = (Zone as any)['SyncTestZoneSpec'];
@ -44,15 +44,15 @@ declare let jest: any;
const symbol = Zone.__symbol__; const symbol = Zone.__symbol__;
// whether patch jasmine clock when in fakeAsync // whether patch jasmine clock when in fakeAsync
const disablePatchingJasmineClock = _global[symbol('fakeAsyncDisablePatchingClock')] === true; const disablePatchingJasmineClock = global[symbol('fakeAsyncDisablePatchingClock')] === true;
// the original variable name fakeAsyncPatchLock is not accurate, so the name will be // the original variable name fakeAsyncPatchLock is not accurate, so the name will be
// fakeAsyncAutoFakeAsyncWhenClockPatched and if this enablePatchingJasmineClock is false, we also // fakeAsyncAutoFakeAsyncWhenClockPatched and if this enablePatchingJasmineClock is false, we also
// automatically disable the auto jump into fakeAsync feature // automatically disable the auto jump into fakeAsync feature
const enableAutoFakeAsyncWhenClockPatched = !disablePatchingJasmineClock && const enableAutoFakeAsyncWhenClockPatched = !disablePatchingJasmineClock &&
((_global[symbol('fakeAsyncPatchLock')] === true) || ((global[symbol('fakeAsyncPatchLock')] === true) ||
(_global[symbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] === true)); (global[symbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] === true));
const ignoreUnhandledRejection = _global[symbol('ignoreUnhandledRejection')] === true; const ignoreUnhandledRejection = global[symbol('ignoreUnhandledRejection')] === true;
if (!ignoreUnhandledRejection) { if (!ignoreUnhandledRejection) {
const globalErrors = (jasmine as any).GlobalErrors; const globalErrors = (jasmine as any).GlobalErrors;
@ -226,13 +226,13 @@ declare let jest: any;
})(attrs.onComplete); })(attrs.onComplete);
} }
const nativeSetTimeout = _global[Zone.__symbol__('setTimeout')]; const nativeSetTimeout = global[Zone.__symbol__('setTimeout')];
const nativeClearTimeout = _global[Zone.__symbol__('clearTimeout')]; const nativeClearTimeout = global[Zone.__symbol__('clearTimeout')];
if (nativeSetTimeout) { if (nativeSetTimeout) {
// should run setTimeout inside jasmine outside of zone // should run setTimeout inside jasmine outside of zone
attrs.timeout = { attrs.timeout = {
setTimeout: nativeSetTimeout ? nativeSetTimeout : _global.setTimeout, setTimeout: nativeSetTimeout ? nativeSetTimeout : global.setTimeout,
clearTimeout: nativeClearTimeout ? nativeClearTimeout : _global.clearTimeout clearTimeout: nativeClearTimeout ? nativeClearTimeout : global.clearTimeout
}; };
} }
@ -291,6 +291,7 @@ declare let jest: any;
// This is the zone which will be used for running individual tests. // This is the zone which will be used for running individual tests.
// It will be a proxy zone, so that the tests function can retroactively install // It will be a proxy zone, so that the tests function can retroactively install
// different zones. // different zones.
// Example:
// - In beforeEach() do childZone = Zone.current.fork(...); // - In beforeEach() do childZone = Zone.current.fork(...);
// - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the // - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the
// zone outside of fakeAsync it will be able to escape the fakeAsync rules. // zone outside of fakeAsync it will be able to escape the fakeAsync rules.
@ -313,4 +314,4 @@ declare let jest: any;
}; };
return ZoneQueueRunner; return ZoneQueueRunner;
})(QueueRunner); })(QueueRunner);
})(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global); });