Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat: add option to disable jasmine clock patch, also rename the flag…
Browse files Browse the repository at this point in the history
… of auto jump in FakeAsyncTest (#1222)
  • Loading branch information
JiaLiPassion authored and vikerman committed Apr 9, 2019
1 parent 62b8525 commit 10e1b0c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 50 deletions.
97 changes: 52 additions & 45 deletions lib/jasmine/jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion test/browser-zone-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions test/node_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion test/test-env-setup-jasmine-no-patch-clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 2 additions & 1 deletion test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 10e1b0c

Please sign in to comment.