|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | 'use strict';
|
10 |
| -(() => { |
| 10 | +import {patchJasmineClock} from './jasmine.clock'; |
| 11 | +Zone.__load_patch('jasmine', (global: any) => { |
11 | 12 | const __extends = function(d: any, b: any) {
|
12 | 13 | for (const p in b)
|
13 | 14 | if (b.hasOwnProperty(p)) d[p] = b[p];
|
|
16 | 17 | }
|
17 | 18 | d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new (__ as any)());
|
18 | 19 | };
|
19 |
| - const _global: any = |
20 |
| - typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global; |
21 | 20 | // Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs
|
22 | 21 | // in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503)
|
23 | 22 | if (!Zone) throw new Error('Missing: zone.js');
|
24 |
| - if (typeof jasmine == 'undefined') throw new Error('Missing: jasmine.js'); |
| 23 | + if (typeof jasmine == 'undefined') { |
| 24 | + // not using jasmine, just return; |
| 25 | + return; |
| 26 | + } |
25 | 27 | if ((jasmine as any)['__zone_patch__'])
|
26 | 28 | throw new Error(`'jasmine' has already been patched with 'Zone'.`);
|
27 | 29 | (jasmine as any)['__zone_patch__'] = true;
|
|
40 | 42 | const symbol = Zone.__symbol__;
|
41 | 43 |
|
42 | 44 | // whether patch jasmine clock when in fakeAsync
|
43 |
| - const enableClockPatch = _global[symbol('fakeAsyncPatchLock')] === true; |
| 45 | + const enableClockPatch = global[symbol('fakeAsyncPatchLock')] === true; |
44 | 46 |
|
45 | 47 | // Monkey patch all of the jasmine DSL so that each function runs in appropriate zone.
|
46 | 48 | const jasmineEnv: any = jasmine.getEnv();
|
|
68 | 70 | };
|
69 | 71 | });
|
70 | 72 |
|
71 |
| - // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so |
72 |
| - // they can work properly in FakeAsyncTest |
73 |
| - const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']); |
74 |
| - (jasmine as any)['clock'] = function() { |
75 |
| - const clock = originalClockFn.apply(this, arguments); |
76 |
| - if (!clock[symbol('patched')]) { |
77 |
| - clock[symbol('patched')] = symbol('patched'); |
78 |
| - const originalTick = (clock[symbol('tick')] = clock.tick); |
79 |
| - clock.tick = function() { |
80 |
| - const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); |
81 |
| - if (fakeAsyncZoneSpec) { |
82 |
| - return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments); |
83 |
| - } |
84 |
| - return originalTick.apply(this, arguments); |
85 |
| - }; |
86 |
| - const originalMockDate = (clock[symbol('mockDate')] = clock.mockDate); |
87 |
| - clock.mockDate = function() { |
88 |
| - const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); |
89 |
| - if (fakeAsyncZoneSpec) { |
90 |
| - const dateTime = arguments.length > 0 ? arguments[0] : new Date(); |
91 |
| - return fakeAsyncZoneSpec.setCurrentRealTime.apply( |
92 |
| - fakeAsyncZoneSpec, |
93 |
| - dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] : |
94 |
| - arguments); |
95 |
| - } |
96 |
| - return originalMockDate.apply(this, arguments); |
97 |
| - }; |
98 |
| - // for auto go into fakeAsync feature, we need the flag to enable it |
99 |
| - if (enableClockPatch) { |
100 |
| - ['install', 'uninstall'].forEach(methodName => { |
101 |
| - const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]); |
102 |
| - clock[methodName] = function() { |
103 |
| - const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec']; |
104 |
| - if (FakeAsyncTestZoneSpec) { |
105 |
| - (jasmine as any)[symbol('clockInstalled')] = 'install' === methodName; |
106 |
| - return; |
107 |
| - } |
108 |
| - return originalClockFn.apply(this, arguments); |
109 |
| - }; |
110 |
| - }); |
111 |
| - } |
112 |
| - } |
113 |
| - return clock; |
114 |
| - }; |
115 |
| - |
| 73 | + patchJasmineClock(jasmine, enableClockPatch); |
116 | 74 | /**
|
117 | 75 | * Gets a function wrapping the body of a Jasmine `describe` block to execute in a
|
118 | 76 | * synchronous-only zone.
|
|
127 | 85 | const isClockInstalled = !!(jasmine as any)[symbol('clockInstalled')];
|
128 | 86 | const testProxyZoneSpec = queueRunner.testProxyZoneSpec;
|
129 | 87 | const testProxyZone = queueRunner.testProxyZone;
|
130 |
| - let lastDelegate; |
131 | 88 | if (isClockInstalled && enableClockPatch) {
|
132 | 89 | // auto run a fakeAsync
|
133 | 90 | const fakeAsyncModule = (Zone as any)[Zone.__symbol__('fakeAsyncTest')];
|
|
177 | 134 | (jasmine as any).QueueRunner = (function(_super) {
|
178 | 135 | __extends(ZoneQueueRunner, _super);
|
179 | 136 | function ZoneQueueRunner(attrs: {
|
180 |
| - onComplete: Function; userContext?: any; |
| 137 | + onComplete: Function; |
| 138 | + userContext?: any; |
181 | 139 | timeout?: {setTimeout: Function; clearTimeout: Function};
|
182 | 140 | onException?: (error: any) => void;
|
183 | 141 | }) {
|
|
188 | 146 | ambientZone.scheduleMicroTask('jasmine.onComplete', fn);
|
189 | 147 | })(attrs.onComplete);
|
190 | 148 |
|
191 |
| - const nativeSetTimeout = _global['__zone_symbol__setTimeout']; |
192 |
| - const nativeClearTimeout = _global['__zone_symbol__clearTimeout']; |
| 149 | + const nativeSetTimeout = global['__zone_symbol__setTimeout']; |
| 150 | + const nativeClearTimeout = global['__zone_symbol__clearTimeout']; |
193 | 151 | if (nativeSetTimeout) {
|
194 | 152 | // should run setTimeout inside jasmine outside of zone
|
195 | 153 | attrs.timeout = {
|
196 |
| - setTimeout: nativeSetTimeout ? nativeSetTimeout : _global.setTimeout, |
197 |
| - clearTimeout: nativeClearTimeout ? nativeClearTimeout : _global.clearTimeout |
| 154 | + setTimeout: nativeSetTimeout ? nativeSetTimeout : global.setTimeout, |
| 155 | + clearTimeout: nativeClearTimeout ? nativeClearTimeout : global.clearTimeout |
198 | 156 | };
|
199 | 157 | }
|
200 | 158 |
|
|
272 | 230 | };
|
273 | 231 | return ZoneQueueRunner;
|
274 | 232 | })(QueueRunner);
|
275 |
| -})(); |
| 233 | +}); |
0 commit comments