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

feat: add option to disable jasmine clock patch, also rename the flag of auto jump in FakeAsyncTest #1222

Merged
merged 1 commit into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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