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

Commit 10e1b0c

Browse files
JiaLiPassionvikerman
authored andcommitted
feat: add option to disable jasmine clock patch, also rename the flag of auto jump in FakeAsyncTest (#1222)
1 parent 62b8525 commit 10e1b0c

File tree

5 files changed

+58
-50
lines changed

5 files changed

+58
-50
lines changed

lib/jasmine/jasmine.ts

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@
4040
const symbol = Zone.__symbol__;
4141

4242
// whether patch jasmine clock when in fakeAsync
43-
const enableClockPatch = _global[symbol('fakeAsyncPatchLock')] === true;
43+
const disablePatchingJasmineClock = _global[symbol('fakeAsyncDisablePatchingClock')] === true;
44+
// the original variable name fakeAsyncPatchLock is not accurate, so the name will be
45+
// fakeAsyncAutoFakeAsyncWhenClockPatched and if this enablePatchingJasmineClock is false, we also
46+
// automatically disable the auto jump into fakeAsync feature
47+
const enableAutoFakeAsyncWhenClockPatched = !disablePatchingJasmineClock &&
48+
((_global[symbol('fakeAsyncPatchLock')] === true) ||
49+
(_global[symbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] === true));
4450

4551
const ignoreUnhandledRejection = _global[symbol('ignoreUnhandledRejection')] === true;
4652

@@ -94,51 +100,52 @@
94100
};
95101
});
96102

97-
// need to patch jasmine.clock().mockDate and jasmine.clock().tick() so
98-
// they can work properly in FakeAsyncTest
99-
const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']);
100-
(jasmine as any)['clock'] = function() {
101-
const clock = originalClockFn.apply(this, arguments);
102-
if (!clock[symbol('patched')]) {
103-
clock[symbol('patched')] = symbol('patched');
104-
const originalTick = (clock[symbol('tick')] = clock.tick);
105-
clock.tick = function() {
106-
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
107-
if (fakeAsyncZoneSpec) {
108-
return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments);
109-
}
110-
return originalTick.apply(this, arguments);
111-
};
112-
const originalMockDate = (clock[symbol('mockDate')] = clock.mockDate);
113-
clock.mockDate = function() {
114-
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
115-
if (fakeAsyncZoneSpec) {
116-
const dateTime = arguments.length > 0 ? arguments[0] : new Date();
117-
return fakeAsyncZoneSpec.setCurrentRealTime.apply(
118-
fakeAsyncZoneSpec,
119-
dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
120-
arguments);
103+
if (!disablePatchingJasmineClock) {
104+
// need to patch jasmine.clock().mockDate and jasmine.clock().tick() so
105+
// they can work properly in FakeAsyncTest
106+
const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']);
107+
(jasmine as any)['clock'] = function() {
108+
const clock = originalClockFn.apply(this, arguments);
109+
if (!clock[symbol('patched')]) {
110+
clock[symbol('patched')] = symbol('patched');
111+
const originalTick = (clock[symbol('tick')] = clock.tick);
112+
clock.tick = function() {
113+
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
114+
if (fakeAsyncZoneSpec) {
115+
return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments);
116+
}
117+
return originalTick.apply(this, arguments);
118+
};
119+
const originalMockDate = (clock[symbol('mockDate')] = clock.mockDate);
120+
clock.mockDate = function() {
121+
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
122+
if (fakeAsyncZoneSpec) {
123+
const dateTime = arguments.length > 0 ? arguments[0] : new Date();
124+
return fakeAsyncZoneSpec.setCurrentRealTime.apply(
125+
fakeAsyncZoneSpec,
126+
dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
127+
arguments);
128+
}
129+
return originalMockDate.apply(this, arguments);
130+
};
131+
// for auto go into fakeAsync feature, we need the flag to enable it
132+
if (enableAutoFakeAsyncWhenClockPatched) {
133+
['install', 'uninstall'].forEach(methodName => {
134+
const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]);
135+
clock[methodName] = function() {
136+
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
137+
if (FakeAsyncTestZoneSpec) {
138+
(jasmine as any)[symbol('clockInstalled')] = 'install' === methodName;
139+
return;
140+
}
141+
return originalClockFn.apply(this, arguments);
142+
};
143+
});
121144
}
122-
return originalMockDate.apply(this, arguments);
123-
};
124-
// for auto go into fakeAsync feature, we need the flag to enable it
125-
if (enableClockPatch) {
126-
['install', 'uninstall'].forEach(methodName => {
127-
const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]);
128-
clock[methodName] = function() {
129-
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
130-
if (FakeAsyncTestZoneSpec) {
131-
(jasmine as any)[symbol('clockInstalled')] = 'install' === methodName;
132-
return;
133-
}
134-
return originalClockFn.apply(this, arguments);
135-
};
136-
});
137145
}
138-
}
139-
return clock;
140-
};
141-
146+
return clock;
147+
};
148+
}
142149
/**
143150
* Gets a function wrapping the body of a Jasmine `describe` block to execute in a
144151
* synchronous-only zone.
@@ -154,7 +161,7 @@
154161
const testProxyZoneSpec = queueRunner.testProxyZoneSpec;
155162
const testProxyZone = queueRunner.testProxyZone;
156163
let lastDelegate;
157-
if (isClockInstalled && enableClockPatch) {
164+
if (isClockInstalled && enableAutoFakeAsyncWhenClockPatched) {
158165
// auto run a fakeAsync
159166
const fakeAsyncModule = (Zone as any)[Zone.__symbol__('fakeAsyncTest')];
160167
if (fakeAsyncModule && typeof fakeAsyncModule.fakeAsync === 'function') {

test/browser-zone-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
if (typeof window !== 'undefined') {
99
(window as any)['__Zone_enable_cross_context_check'] = true;
10-
(window as any)['__zone_symbol__fakeAsyncPatchLock'] = true;
10+
(window as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] = true;
1111
}
1212
import '../lib/common/to-string';
1313
import '../lib/browser/api-util';

test/node_entry_point.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
// Must be loaded before zone loads, so that zone can detect WTF.
1010
if (typeof global !== 'undefined' &&
11-
(global as any)['__zone_symbol__fakeAsyncPatchLock'] !== false) {
12-
(global as any)['__zone_symbol__fakeAsyncPatchLock'] = true;
11+
(global as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] !== false) {
12+
(global as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] = true;
1313
}
1414
import './wtf_mock';
1515
import './test_fake_polyfill';

test/test-env-setup-jasmine-no-patch-clock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
(global as any)['__zone_symbol__fakeAsyncPatchLock'] = false;
8+
(global as any)['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'] = false;

test/zone-spec/fake-async-test.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function supportNode() {
2222

2323
function supportClock() {
2424
const _global: any = typeof window === 'undefined' ? global : window;
25-
return typeof jasmine.clock === 'function' && _global['__zone_symbol__fakeAsyncPatchLock'];
25+
return typeof jasmine.clock === 'function' &&
26+
_global['__zone_symbol__fakeAsyncAutoFakeAsyncWhenClockPatched'];
2627
}
2728

2829
(supportClock as any).message = 'support patch clock';

0 commit comments

Comments
 (0)