From b78c987b66cee5dfb238c8332475f4a61029e707 Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Wed, 10 Apr 2019 08:16:57 +0900 Subject: [PATCH] feat: add option to disable jasmine clock patch, also rename the flag of auto jump in FakeAsyncTest --- lib/jasmine/jasmine.ts | 97 ++++++++++--------- test/browser-zone-setup.ts | 2 +- test/node_entry_point.ts | 4 +- test/test-env-setup-jasmine-no-patch-clock.ts | 2 +- test/zone-spec/fake-async-test.spec.ts | 3 +- 5 files changed, 58 insertions(+), 50 deletions(-) diff --git a/lib/jasmine/jasmine.ts b/lib/jasmine/jasmine.ts index 9bd387fdd..d75800992 100644 --- a/lib/jasmine/jasmine.ts +++ b/lib/jasmine/jasmine.ts @@ -40,7 +40,13 @@ const symbol = Zone.__symbol__; // whether patch jasmine clock when in fakeAsync - const enableClockPatch = _global[symbol('fakeAsyncPatchLock')] === true; + const disablePatchingJasmineClock = _global[symbol('fakeAsyncDisablePatchingClock')] === true; + // the original variable name fakeAsyncPatchLock is not accurate, so the name will be + // fakeAsyncAutoFakeAsyncWhenClockPatched and if this enablePatchingJasmineClock is false, we also + // automatically disable the auto jump into fakeAsync feature + const enableAutoFakeAsyncWhenClockPatched = !disablePatchingJasmineClock && + ((_global[symbol('fakeAsyncPatchLock')] === true) || + (_global[symbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] === true)); const ignoreUnhandledRejection = _global[symbol('ignoreUnhandledRejection')] === true; @@ -94,51 +100,52 @@ }; }); - // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so - // they can work properly in FakeAsyncTest - const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']); - (jasmine as any)['clock'] = function() { - const clock = originalClockFn.apply(this, arguments); - if (!clock[symbol('patched')]) { - clock[symbol('patched')] = symbol('patched'); - const originalTick = (clock[symbol('tick')] = clock.tick); - clock.tick = function() { - const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); - if (fakeAsyncZoneSpec) { - return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments); - } - return originalTick.apply(this, arguments); - }; - const originalMockDate = (clock[symbol('mockDate')] = clock.mockDate); - clock.mockDate = function() { - const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); - if (fakeAsyncZoneSpec) { - const dateTime = arguments.length > 0 ? arguments[0] : new Date(); - return fakeAsyncZoneSpec.setCurrentRealTime.apply( - fakeAsyncZoneSpec, - dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] : - arguments); + if (!disablePatchingJasmineClock) { + // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so + // they can work properly in FakeAsyncTest + const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']); + (jasmine as any)['clock'] = function() { + const clock = originalClockFn.apply(this, arguments); + if (!clock[symbol('patched')]) { + clock[symbol('patched')] = symbol('patched'); + const originalTick = (clock[symbol('tick')] = clock.tick); + clock.tick = function() { + const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); + if (fakeAsyncZoneSpec) { + return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments); + } + return originalTick.apply(this, arguments); + }; + const originalMockDate = (clock[symbol('mockDate')] = clock.mockDate); + clock.mockDate = function() { + const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); + if (fakeAsyncZoneSpec) { + const dateTime = arguments.length > 0 ? arguments[0] : new Date(); + return fakeAsyncZoneSpec.setCurrentRealTime.apply( + fakeAsyncZoneSpec, + dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] : + arguments); + } + return originalMockDate.apply(this, arguments); + }; + // for auto go into fakeAsync feature, we need the flag to enable it + if (enableAutoFakeAsyncWhenClockPatched) { + ['install', 'uninstall'].forEach(methodName => { + const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]); + clock[methodName] = function() { + const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec']; + if (FakeAsyncTestZoneSpec) { + (jasmine as any)[symbol('clockInstalled')] = 'install' === methodName; + return; + } + return originalClockFn.apply(this, arguments); + }; + }); } - return originalMockDate.apply(this, arguments); - }; - // for auto go into fakeAsync feature, we need the flag to enable it - if (enableClockPatch) { - ['install', 'uninstall'].forEach(methodName => { - const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]); - clock[methodName] = function() { - const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec']; - if (FakeAsyncTestZoneSpec) { - (jasmine as any)[symbol('clockInstalled')] = 'install' === methodName; - return; - } - return originalClockFn.apply(this, arguments); - }; - }); } - } - return clock; - }; - + return clock; + }; + } /** * Gets a function wrapping the body of a Jasmine `describe` block to execute in a * synchronous-only zone. @@ -154,7 +161,7 @@ const testProxyZoneSpec = queueRunner.testProxyZoneSpec; const testProxyZone = queueRunner.testProxyZone; let lastDelegate; - if (isClockInstalled && enableClockPatch) { + if (isClockInstalled && enableAutoFakeAsyncWhenClockPatched) { // auto run a fakeAsync const fakeAsyncModule = (Zone as any)[Zone.__symbol__('fakeAsyncTest')]; if (fakeAsyncModule && typeof fakeAsyncModule.fakeAsync === 'function') { diff --git a/test/browser-zone-setup.ts b/test/browser-zone-setup.ts index 7982c1df5..2e157175d 100644 --- a/test/browser-zone-setup.ts +++ b/test/browser-zone-setup.ts @@ -7,7 +7,7 @@ */ if (typeof window !== 'undefined') { (window as any)['__Zone_enable_cross_context_check'] = true; - (window as any)['__zone_symbol__fakeAsyncPatchLock'] = true; + (window as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] = true; } import '../lib/common/to-string'; import '../lib/browser/api-util'; diff --git a/test/node_entry_point.ts b/test/node_entry_point.ts index 7d7c4ee79..74d27b086 100644 --- a/test/node_entry_point.ts +++ b/test/node_entry_point.ts @@ -8,8 +8,8 @@ // Must be loaded before zone loads, so that zone can detect WTF. if (typeof global !== 'undefined' && - (global as any)['__zone_symbol__fakeAsyncPatchLock'] !== false) { - (global as any)['__zone_symbol__fakeAsyncPatchLock'] = true; + (global as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] !== false) { + (global as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] = true; } import './wtf_mock'; import './test_fake_polyfill'; diff --git a/test/test-env-setup-jasmine-no-patch-clock.ts b/test/test-env-setup-jasmine-no-patch-clock.ts index 8c06e92c1..d847c2fed 100644 --- a/test/test-env-setup-jasmine-no-patch-clock.ts +++ b/test/test-env-setup-jasmine-no-patch-clock.ts @@ -5,4 +5,4 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -(global as any)['__zone_symbol__fakeAsyncPatchLock'] = false; +(global as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] = false; diff --git a/test/zone-spec/fake-async-test.spec.ts b/test/zone-spec/fake-async-test.spec.ts index c58109ccd..082c766c0 100644 --- a/test/zone-spec/fake-async-test.spec.ts +++ b/test/zone-spec/fake-async-test.spec.ts @@ -22,7 +22,8 @@ function supportNode() { function supportClock() { const _global: any = typeof window === 'undefined' ? global : window; - return typeof jasmine.clock === 'function' && _global['__zone_symbol__fakeAsyncPatchLock']; + return typeof jasmine.clock === 'function' && + _global['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched']; } (supportClock as any).message = 'support patch clock';