source: trip-planner-front/node_modules/zone.js/fesm2015/zone-testing.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 88.5 KB
Line 
1'use strict';
2/**
3 * @license Angular v12.0.0-next.0
4 * (c) 2010-2020 Google LLC. https://angular.io/
5 * License: MIT
6 */
7/**
8 * @license
9 * Copyright Google LLC All Rights Reserved.
10 *
11 * Use of this source code is governed by an MIT-style license that can be
12 * found in the LICENSE file at https://angular.io/license
13 */
14/**
15 * @fileoverview
16 * @suppress {globalThis}
17 */
18const NEWLINE = '\n';
19const IGNORE_FRAMES = {};
20const creationTrace = '__creationTrace__';
21const ERROR_TAG = 'STACKTRACE TRACKING';
22const SEP_TAG = '__SEP_TAG__';
23let sepTemplate = SEP_TAG + '@[native]';
24class LongStackTrace {
25 constructor() {
26 this.error = getStacktrace();
27 this.timestamp = new Date();
28 }
29}
30function getStacktraceWithUncaughtError() {
31 return new Error(ERROR_TAG);
32}
33function getStacktraceWithCaughtError() {
34 try {
35 throw getStacktraceWithUncaughtError();
36 }
37 catch (err) {
38 return err;
39 }
40}
41// Some implementations of exception handling don't create a stack trace if the exception
42// isn't thrown, however it's faster not to actually throw the exception.
43const error = getStacktraceWithUncaughtError();
44const caughtError = getStacktraceWithCaughtError();
45const getStacktrace = error.stack ?
46 getStacktraceWithUncaughtError :
47 (caughtError.stack ? getStacktraceWithCaughtError : getStacktraceWithUncaughtError);
48function getFrames(error) {
49 return error.stack ? error.stack.split(NEWLINE) : [];
50}
51function addErrorStack(lines, error) {
52 let trace = getFrames(error);
53 for (let i = 0; i < trace.length; i++) {
54 const frame = trace[i];
55 // Filter out the Frames which are part of stack capturing.
56 if (!IGNORE_FRAMES.hasOwnProperty(frame)) {
57 lines.push(trace[i]);
58 }
59 }
60}
61function renderLongStackTrace(frames, stack) {
62 const longTrace = [stack ? stack.trim() : ''];
63 if (frames) {
64 let timestamp = new Date().getTime();
65 for (let i = 0; i < frames.length; i++) {
66 const traceFrames = frames[i];
67 const lastTime = traceFrames.timestamp;
68 let separator = `____________________Elapsed ${timestamp - lastTime.getTime()} ms; At: ${lastTime}`;
69 separator = separator.replace(/[^\w\d]/g, '_');
70 longTrace.push(sepTemplate.replace(SEP_TAG, separator));
71 addErrorStack(longTrace, traceFrames.error);
72 timestamp = lastTime.getTime();
73 }
74 }
75 return longTrace.join(NEWLINE);
76}
77// if Error.stackTraceLimit is 0, means stack trace
78// is disabled, so we don't need to generate long stack trace
79// this will improve performance in some test(some test will
80// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
81function stackTracesEnabled() {
82 // Cast through any since this property only exists on Error in the nodejs
83 // typings.
84 return Error.stackTraceLimit > 0;
85}
86Zone['longStackTraceZoneSpec'] = {
87 name: 'long-stack-trace',
88 longStackTraceLimit: 10,
89 // add a getLongStackTrace method in spec to
90 // handle handled reject promise error.
91 getLongStackTrace: function (error) {
92 if (!error) {
93 return undefined;
94 }
95 const trace = error[Zone.__symbol__('currentTaskTrace')];
96 if (!trace) {
97 return error.stack;
98 }
99 return renderLongStackTrace(trace, error.stack);
100 },
101 onScheduleTask: function (parentZoneDelegate, currentZone, targetZone, task) {
102 if (stackTracesEnabled()) {
103 const currentTask = Zone.currentTask;
104 let trace = currentTask && currentTask.data && currentTask.data[creationTrace] || [];
105 trace = [new LongStackTrace()].concat(trace);
106 if (trace.length > this.longStackTraceLimit) {
107 trace.length = this.longStackTraceLimit;
108 }
109 if (!task.data)
110 task.data = {};
111 if (task.type === 'eventTask') {
112 // Fix issue https://github.com/angular/zone.js/issues/1195,
113 // For event task of browser, by default, all task will share a
114 // singleton instance of data object, we should create a new one here
115 // The cast to `any` is required to workaround a closure bug which wrongly applies
116 // URL sanitization rules to .data access.
117 task.data = Object.assign({}, task.data);
118 }
119 task.data[creationTrace] = trace;
120 }
121 return parentZoneDelegate.scheduleTask(targetZone, task);
122 },
123 onHandleError: function (parentZoneDelegate, currentZone, targetZone, error) {
124 if (stackTracesEnabled()) {
125 const parentTask = Zone.currentTask || error.task;
126 if (error instanceof Error && parentTask) {
127 const longStack = renderLongStackTrace(parentTask.data && parentTask.data[creationTrace], error.stack);
128 try {
129 error.stack = error.longStack = longStack;
130 }
131 catch (err) {
132 }
133 }
134 }
135 return parentZoneDelegate.handleError(targetZone, error);
136 }
137};
138function captureStackTraces(stackTraces, count) {
139 if (count > 0) {
140 stackTraces.push(getFrames((new LongStackTrace()).error));
141 captureStackTraces(stackTraces, count - 1);
142 }
143}
144function computeIgnoreFrames() {
145 if (!stackTracesEnabled()) {
146 return;
147 }
148 const frames = [];
149 captureStackTraces(frames, 2);
150 const frames1 = frames[0];
151 const frames2 = frames[1];
152 for (let i = 0; i < frames1.length; i++) {
153 const frame1 = frames1[i];
154 if (frame1.indexOf(ERROR_TAG) == -1) {
155 let match = frame1.match(/^\s*at\s+/);
156 if (match) {
157 sepTemplate = match[0] + SEP_TAG + ' (http://localhost)';
158 break;
159 }
160 }
161 }
162 for (let i = 0; i < frames1.length; i++) {
163 const frame1 = frames1[i];
164 const frame2 = frames2[i];
165 if (frame1 === frame2) {
166 IGNORE_FRAMES[frame1] = true;
167 }
168 else {
169 break;
170 }
171 }
172}
173computeIgnoreFrames();
174
175/**
176 * @license
177 * Copyright Google LLC All Rights Reserved.
178 *
179 * Use of this source code is governed by an MIT-style license that can be
180 * found in the LICENSE file at https://angular.io/license
181 */
182class ProxyZoneSpec {
183 constructor(defaultSpecDelegate = null) {
184 this.defaultSpecDelegate = defaultSpecDelegate;
185 this.name = 'ProxyZone';
186 this._delegateSpec = null;
187 this.properties = { 'ProxyZoneSpec': this };
188 this.propertyKeys = null;
189 this.lastTaskState = null;
190 this.isNeedToTriggerHasTask = false;
191 this.tasks = [];
192 this.setDelegate(defaultSpecDelegate);
193 }
194 static get() {
195 return Zone.current.get('ProxyZoneSpec');
196 }
197 static isLoaded() {
198 return ProxyZoneSpec.get() instanceof ProxyZoneSpec;
199 }
200 static assertPresent() {
201 if (!ProxyZoneSpec.isLoaded()) {
202 throw new Error(`Expected to be running in 'ProxyZone', but it was not found.`);
203 }
204 return ProxyZoneSpec.get();
205 }
206 setDelegate(delegateSpec) {
207 const isNewDelegate = this._delegateSpec !== delegateSpec;
208 this._delegateSpec = delegateSpec;
209 this.propertyKeys && this.propertyKeys.forEach((key) => delete this.properties[key]);
210 this.propertyKeys = null;
211 if (delegateSpec && delegateSpec.properties) {
212 this.propertyKeys = Object.keys(delegateSpec.properties);
213 this.propertyKeys.forEach((k) => this.properties[k] = delegateSpec.properties[k]);
214 }
215 // if a new delegateSpec was set, check if we need to trigger hasTask
216 if (isNewDelegate && this.lastTaskState &&
217 (this.lastTaskState.macroTask || this.lastTaskState.microTask)) {
218 this.isNeedToTriggerHasTask = true;
219 }
220 }
221 getDelegate() {
222 return this._delegateSpec;
223 }
224 resetDelegate() {
225 const delegateSpec = this.getDelegate();
226 this.setDelegate(this.defaultSpecDelegate);
227 }
228 tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone) {
229 if (this.isNeedToTriggerHasTask && this.lastTaskState) {
230 // last delegateSpec has microTask or macroTask
231 // should call onHasTask in current delegateSpec
232 this.isNeedToTriggerHasTask = false;
233 this.onHasTask(parentZoneDelegate, currentZone, targetZone, this.lastTaskState);
234 }
235 }
236 removeFromTasks(task) {
237 if (!this.tasks) {
238 return;
239 }
240 for (let i = 0; i < this.tasks.length; i++) {
241 if (this.tasks[i] === task) {
242 this.tasks.splice(i, 1);
243 return;
244 }
245 }
246 }
247 getAndClearPendingTasksInfo() {
248 if (this.tasks.length === 0) {
249 return '';
250 }
251 const taskInfo = this.tasks.map((task) => {
252 const dataInfo = task.data &&
253 Object.keys(task.data)
254 .map((key) => {
255 return key + ':' + task.data[key];
256 })
257 .join(',');
258 return `type: ${task.type}, source: ${task.source}, args: {${dataInfo}}`;
259 });
260 const pendingTasksInfo = '--Pending async tasks are: [' + taskInfo + ']';
261 // clear tasks
262 this.tasks = [];
263 return pendingTasksInfo;
264 }
265 onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec) {
266 if (this._delegateSpec && this._delegateSpec.onFork) {
267 return this._delegateSpec.onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec);
268 }
269 else {
270 return parentZoneDelegate.fork(targetZone, zoneSpec);
271 }
272 }
273 onIntercept(parentZoneDelegate, currentZone, targetZone, delegate, source) {
274 if (this._delegateSpec && this._delegateSpec.onIntercept) {
275 return this._delegateSpec.onIntercept(parentZoneDelegate, currentZone, targetZone, delegate, source);
276 }
277 else {
278 return parentZoneDelegate.intercept(targetZone, delegate, source);
279 }
280 }
281 onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
282 this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
283 if (this._delegateSpec && this._delegateSpec.onInvoke) {
284 return this._delegateSpec.onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source);
285 }
286 else {
287 return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
288 }
289 }
290 onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
291 if (this._delegateSpec && this._delegateSpec.onHandleError) {
292 return this._delegateSpec.onHandleError(parentZoneDelegate, currentZone, targetZone, error);
293 }
294 else {
295 return parentZoneDelegate.handleError(targetZone, error);
296 }
297 }
298 onScheduleTask(parentZoneDelegate, currentZone, targetZone, task) {
299 if (task.type !== 'eventTask') {
300 this.tasks.push(task);
301 }
302 if (this._delegateSpec && this._delegateSpec.onScheduleTask) {
303 return this._delegateSpec.onScheduleTask(parentZoneDelegate, currentZone, targetZone, task);
304 }
305 else {
306 return parentZoneDelegate.scheduleTask(targetZone, task);
307 }
308 }
309 onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) {
310 if (task.type !== 'eventTask') {
311 this.removeFromTasks(task);
312 }
313 this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
314 if (this._delegateSpec && this._delegateSpec.onInvokeTask) {
315 return this._delegateSpec.onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs);
316 }
317 else {
318 return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
319 }
320 }
321 onCancelTask(parentZoneDelegate, currentZone, targetZone, task) {
322 if (task.type !== 'eventTask') {
323 this.removeFromTasks(task);
324 }
325 this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
326 if (this._delegateSpec && this._delegateSpec.onCancelTask) {
327 return this._delegateSpec.onCancelTask(parentZoneDelegate, currentZone, targetZone, task);
328 }
329 else {
330 return parentZoneDelegate.cancelTask(targetZone, task);
331 }
332 }
333 onHasTask(delegate, current, target, hasTaskState) {
334 this.lastTaskState = hasTaskState;
335 if (this._delegateSpec && this._delegateSpec.onHasTask) {
336 this._delegateSpec.onHasTask(delegate, current, target, hasTaskState);
337 }
338 else {
339 delegate.hasTask(target, hasTaskState);
340 }
341 }
342}
343// Export the class so that new instances can be created with proper
344// constructor params.
345Zone['ProxyZoneSpec'] = ProxyZoneSpec;
346
347/**
348 * @license
349 * Copyright Google LLC All Rights Reserved.
350 *
351 * Use of this source code is governed by an MIT-style license that can be
352 * found in the LICENSE file at https://angular.io/license
353 */
354class SyncTestZoneSpec {
355 constructor(namePrefix) {
356 this.runZone = Zone.current;
357 this.name = 'syncTestZone for ' + namePrefix;
358 }
359 onScheduleTask(delegate, current, target, task) {
360 switch (task.type) {
361 case 'microTask':
362 case 'macroTask':
363 throw new Error(`Cannot call ${task.source} from within a sync test.`);
364 case 'eventTask':
365 task = delegate.scheduleTask(target, task);
366 break;
367 }
368 return task;
369 }
370}
371// Export the class so that new instances can be created with proper
372// constructor params.
373Zone['SyncTestZoneSpec'] = SyncTestZoneSpec;
374
375/**
376 * @license
377 * Copyright Google LLC All Rights Reserved.
378 *
379 * Use of this source code is governed by an MIT-style license that can be
380 * found in the LICENSE file at https://angular.io/license
381 */
382Zone.__load_patch('jasmine', (global, Zone, api) => {
383 const __extends = function (d, b) {
384 for (const p in b)
385 if (b.hasOwnProperty(p))
386 d[p] = b[p];
387 function __() {
388 this.constructor = d;
389 }
390 d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
391 };
392 // Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs
393 // in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503)
394 if (!Zone)
395 throw new Error('Missing: zone.js');
396 if (typeof jest !== 'undefined') {
397 // return if jasmine is a light implementation inside jest
398 // in this case, we are running inside jest not jasmine
399 return;
400 }
401 if (typeof jasmine == 'undefined' || jasmine['__zone_patch__']) {
402 return;
403 }
404 jasmine['__zone_patch__'] = true;
405 const SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
406 const ProxyZoneSpec = Zone['ProxyZoneSpec'];
407 if (!SyncTestZoneSpec)
408 throw new Error('Missing: SyncTestZoneSpec');
409 if (!ProxyZoneSpec)
410 throw new Error('Missing: ProxyZoneSpec');
411 const ambientZone = Zone.current;
412 // Create a synchronous-only zone in which to run `describe` blocks in order to raise an
413 // error if any asynchronous operations are attempted inside of a `describe` but outside of
414 // a `beforeEach` or `it`.
415 const syncZone = ambientZone.fork(new SyncTestZoneSpec('jasmine.describe'));
416 const symbol = Zone.__symbol__;
417 // whether patch jasmine clock when in fakeAsync
418 const disablePatchingJasmineClock = global[symbol('fakeAsyncDisablePatchingClock')] === true;
419 // the original variable name fakeAsyncPatchLock is not accurate, so the name will be
420 // fakeAsyncAutoFakeAsyncWhenClockPatched and if this enablePatchingJasmineClock is false, we also
421 // automatically disable the auto jump into fakeAsync feature
422 const enableAutoFakeAsyncWhenClockPatched = !disablePatchingJasmineClock &&
423 ((global[symbol('fakeAsyncPatchLock')] === true) ||
424 (global[symbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] === true));
425 const ignoreUnhandledRejection = global[symbol('ignoreUnhandledRejection')] === true;
426 if (!ignoreUnhandledRejection) {
427 const globalErrors = jasmine.GlobalErrors;
428 if (globalErrors && !jasmine[symbol('GlobalErrors')]) {
429 jasmine[symbol('GlobalErrors')] = globalErrors;
430 jasmine.GlobalErrors = function () {
431 const instance = new globalErrors();
432 const originalInstall = instance.install;
433 if (originalInstall && !instance[symbol('install')]) {
434 instance[symbol('install')] = originalInstall;
435 instance.install = function () {
436 const originalHandlers = process.listeners('unhandledRejection');
437 const r = originalInstall.apply(this, arguments);
438 process.removeAllListeners('unhandledRejection');
439 if (originalHandlers) {
440 originalHandlers.forEach(h => process.on('unhandledRejection', h));
441 }
442 return r;
443 };
444 }
445 return instance;
446 };
447 }
448 }
449 // Monkey patch all of the jasmine DSL so that each function runs in appropriate zone.
450 const jasmineEnv = jasmine.getEnv();
451 ['describe', 'xdescribe', 'fdescribe'].forEach(methodName => {
452 let originalJasmineFn = jasmineEnv[methodName];
453 jasmineEnv[methodName] = function (description, specDefinitions) {
454 return originalJasmineFn.call(this, description, wrapDescribeInZone(specDefinitions));
455 };
456 });
457 ['it', 'xit', 'fit'].forEach(methodName => {
458 let originalJasmineFn = jasmineEnv[methodName];
459 jasmineEnv[symbol(methodName)] = originalJasmineFn;
460 jasmineEnv[methodName] = function (description, specDefinitions, timeout) {
461 arguments[1] = wrapTestInZone(specDefinitions);
462 return originalJasmineFn.apply(this, arguments);
463 };
464 });
465 ['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(methodName => {
466 let originalJasmineFn = jasmineEnv[methodName];
467 jasmineEnv[symbol(methodName)] = originalJasmineFn;
468 jasmineEnv[methodName] = function (specDefinitions, timeout) {
469 arguments[0] = wrapTestInZone(specDefinitions);
470 return originalJasmineFn.apply(this, arguments);
471 };
472 });
473 if (!disablePatchingJasmineClock) {
474 // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so
475 // they can work properly in FakeAsyncTest
476 const originalClockFn = (jasmine[symbol('clock')] = jasmine['clock']);
477 jasmine['clock'] = function () {
478 const clock = originalClockFn.apply(this, arguments);
479 if (!clock[symbol('patched')]) {
480 clock[symbol('patched')] = symbol('patched');
481 const originalTick = (clock[symbol('tick')] = clock.tick);
482 clock.tick = function () {
483 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
484 if (fakeAsyncZoneSpec) {
485 return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments);
486 }
487 return originalTick.apply(this, arguments);
488 };
489 const originalMockDate = (clock[symbol('mockDate')] = clock.mockDate);
490 clock.mockDate = function () {
491 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
492 if (fakeAsyncZoneSpec) {
493 const dateTime = arguments.length > 0 ? arguments[0] : new Date();
494 return fakeAsyncZoneSpec.setFakeBaseSystemTime.apply(fakeAsyncZoneSpec, dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
495 arguments);
496 }
497 return originalMockDate.apply(this, arguments);
498 };
499 // for auto go into fakeAsync feature, we need the flag to enable it
500 if (enableAutoFakeAsyncWhenClockPatched) {
501 ['install', 'uninstall'].forEach(methodName => {
502 const originalClockFn = (clock[symbol(methodName)] = clock[methodName]);
503 clock[methodName] = function () {
504 const FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec'];
505 if (FakeAsyncTestZoneSpec) {
506 jasmine[symbol('clockInstalled')] = 'install' === methodName;
507 return;
508 }
509 return originalClockFn.apply(this, arguments);
510 };
511 });
512 }
513 }
514 return clock;
515 };
516 }
517 // monkey patch createSpyObj to make properties enumerable to true
518 if (!jasmine[Zone.__symbol__('createSpyObj')]) {
519 const originalCreateSpyObj = jasmine.createSpyObj;
520 jasmine[Zone.__symbol__('createSpyObj')] = originalCreateSpyObj;
521 jasmine.createSpyObj = function () {
522 const args = Array.prototype.slice.call(arguments);
523 const propertyNames = args.length >= 3 ? args[2] : null;
524 let spyObj;
525 if (propertyNames) {
526 const defineProperty = Object.defineProperty;
527 Object.defineProperty = function (obj, p, attributes) {
528 return defineProperty.call(this, obj, p, Object.assign(Object.assign({}, attributes), { configurable: true, enumerable: true }));
529 };
530 try {
531 spyObj = originalCreateSpyObj.apply(this, args);
532 }
533 finally {
534 Object.defineProperty = defineProperty;
535 }
536 }
537 else {
538 spyObj = originalCreateSpyObj.apply(this, args);
539 }
540 return spyObj;
541 };
542 }
543 /**
544 * Gets a function wrapping the body of a Jasmine `describe` block to execute in a
545 * synchronous-only zone.
546 */
547 function wrapDescribeInZone(describeBody) {
548 return function () {
549 return syncZone.run(describeBody, this, arguments);
550 };
551 }
552 function runInTestZone(testBody, applyThis, queueRunner, done) {
553 const isClockInstalled = !!jasmine[symbol('clockInstalled')];
554 const testProxyZoneSpec = queueRunner.testProxyZoneSpec;
555 const testProxyZone = queueRunner.testProxyZone;
556 if (isClockInstalled && enableAutoFakeAsyncWhenClockPatched) {
557 // auto run a fakeAsync
558 const fakeAsyncModule = Zone[Zone.__symbol__('fakeAsyncTest')];
559 if (fakeAsyncModule && typeof fakeAsyncModule.fakeAsync === 'function') {
560 testBody = fakeAsyncModule.fakeAsync(testBody);
561 }
562 }
563 if (done) {
564 return testProxyZone.run(testBody, applyThis, [done]);
565 }
566 else {
567 return testProxyZone.run(testBody, applyThis);
568 }
569 }
570 /**
571 * Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to
572 * execute in a ProxyZone zone.
573 * This will run in `testProxyZone`. The `testProxyZone` will be reset by the `ZoneQueueRunner`
574 */
575 function wrapTestInZone(testBody) {
576 // The `done` callback is only passed through if the function expects at least one argument.
577 // Note we have to make a function with correct number of arguments, otherwise jasmine will
578 // think that all functions are sync or async.
579 return (testBody && (testBody.length ? function (done) {
580 return runInTestZone(testBody, this, this.queueRunner, done);
581 } : function () {
582 return runInTestZone(testBody, this, this.queueRunner);
583 }));
584 }
585 const QueueRunner = jasmine.QueueRunner;
586 jasmine.QueueRunner = (function (_super) {
587 __extends(ZoneQueueRunner, _super);
588 function ZoneQueueRunner(attrs) {
589 if (attrs.onComplete) {
590 attrs.onComplete = (fn => () => {
591 // All functions are done, clear the test zone.
592 this.testProxyZone = null;
593 this.testProxyZoneSpec = null;
594 ambientZone.scheduleMicroTask('jasmine.onComplete', fn);
595 })(attrs.onComplete);
596 }
597 const nativeSetTimeout = global[Zone.__symbol__('setTimeout')];
598 const nativeClearTimeout = global[Zone.__symbol__('clearTimeout')];
599 if (nativeSetTimeout) {
600 // should run setTimeout inside jasmine outside of zone
601 attrs.timeout = {
602 setTimeout: nativeSetTimeout ? nativeSetTimeout : global.setTimeout,
603 clearTimeout: nativeClearTimeout ? nativeClearTimeout : global.clearTimeout
604 };
605 }
606 // create a userContext to hold the queueRunner itself
607 // so we can access the testProxy in it/xit/beforeEach ...
608 if (jasmine.UserContext) {
609 if (!attrs.userContext) {
610 attrs.userContext = new jasmine.UserContext();
611 }
612 attrs.userContext.queueRunner = this;
613 }
614 else {
615 if (!attrs.userContext) {
616 attrs.userContext = {};
617 }
618 attrs.userContext.queueRunner = this;
619 }
620 // patch attrs.onException
621 const onException = attrs.onException;
622 attrs.onException = function (error) {
623 if (error &&
624 error.message ===
625 'Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.') {
626 // jasmine timeout, we can make the error message more
627 // reasonable to tell what tasks are pending
628 const proxyZoneSpec = this && this.testProxyZoneSpec;
629 if (proxyZoneSpec) {
630 const pendingTasksInfo = proxyZoneSpec.getAndClearPendingTasksInfo();
631 try {
632 // try catch here in case error.message is not writable
633 error.message += pendingTasksInfo;
634 }
635 catch (err) {
636 }
637 }
638 }
639 if (onException) {
640 onException.call(this, error);
641 }
642 };
643 _super.call(this, attrs);
644 }
645 ZoneQueueRunner.prototype.execute = function () {
646 let zone = Zone.current;
647 let isChildOfAmbientZone = false;
648 while (zone) {
649 if (zone === ambientZone) {
650 isChildOfAmbientZone = true;
651 break;
652 }
653 zone = zone.parent;
654 }
655 if (!isChildOfAmbientZone)
656 throw new Error('Unexpected Zone: ' + Zone.current.name);
657 // This is the zone which will be used for running individual tests.
658 // It will be a proxy zone, so that the tests function can retroactively install
659 // different zones.
660 // Example:
661 // - In beforeEach() do childZone = Zone.current.fork(...);
662 // - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the
663 // zone outside of fakeAsync it will be able to escape the fakeAsync rules.
664 // - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add
665 // fakeAsync behavior to the childZone.
666 this.testProxyZoneSpec = new ProxyZoneSpec();
667 this.testProxyZone = ambientZone.fork(this.testProxyZoneSpec);
668 if (!Zone.currentTask) {
669 // if we are not running in a task then if someone would register a
670 // element.addEventListener and then calling element.click() the
671 // addEventListener callback would think that it is the top most task and would
672 // drain the microtask queue on element.click() which would be incorrect.
673 // For this reason we always force a task when running jasmine tests.
674 Zone.current.scheduleMicroTask('jasmine.execute().forceTask', () => QueueRunner.prototype.execute.call(this));
675 }
676 else {
677 _super.prototype.execute.call(this);
678 }
679 };
680 return ZoneQueueRunner;
681 })(QueueRunner);
682});
683
684/**
685 * @license
686 * Copyright Google LLC All Rights Reserved.
687 *
688 * Use of this source code is governed by an MIT-style license that can be
689 * found in the LICENSE file at https://angular.io/license
690 */
691Zone.__load_patch('jest', (context, Zone, api) => {
692 if (typeof jest === 'undefined' || jest['__zone_patch__']) {
693 return;
694 }
695 jest['__zone_patch__'] = true;
696 const ProxyZoneSpec = Zone['ProxyZoneSpec'];
697 const SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
698 if (!ProxyZoneSpec) {
699 throw new Error('Missing ProxyZoneSpec');
700 }
701 const rootZone = Zone.current;
702 const syncZone = rootZone.fork(new SyncTestZoneSpec('jest.describe'));
703 const proxyZoneSpec = new ProxyZoneSpec();
704 const proxyZone = rootZone.fork(proxyZoneSpec);
705 function wrapDescribeFactoryInZone(originalJestFn) {
706 return function (...tableArgs) {
707 const originalDescribeFn = originalJestFn.apply(this, tableArgs);
708 return function (...args) {
709 args[1] = wrapDescribeInZone(args[1]);
710 return originalDescribeFn.apply(this, args);
711 };
712 };
713 }
714 function wrapTestFactoryInZone(originalJestFn) {
715 return function (...tableArgs) {
716 return function (...args) {
717 args[1] = wrapTestInZone(args[1]);
718 return originalJestFn.apply(this, tableArgs).apply(this, args);
719 };
720 };
721 }
722 /**
723 * Gets a function wrapping the body of a jest `describe` block to execute in a
724 * synchronous-only zone.
725 */
726 function wrapDescribeInZone(describeBody) {
727 return function (...args) {
728 return syncZone.run(describeBody, this, args);
729 };
730 }
731 /**
732 * Gets a function wrapping the body of a jest `it/beforeEach/afterEach` block to
733 * execute in a ProxyZone zone.
734 * This will run in the `proxyZone`.
735 */
736 function wrapTestInZone(testBody, isTestFunc = false) {
737 if (typeof testBody !== 'function') {
738 return testBody;
739 }
740 const wrappedFunc = function () {
741 if (Zone[api.symbol('useFakeTimersCalled')] === true && testBody &&
742 !testBody.isFakeAsync) {
743 // jest.useFakeTimers is called, run into fakeAsyncTest automatically.
744 const fakeAsyncModule = Zone[Zone.__symbol__('fakeAsyncTest')];
745 if (fakeAsyncModule && typeof fakeAsyncModule.fakeAsync === 'function') {
746 testBody = fakeAsyncModule.fakeAsync(testBody);
747 }
748 }
749 proxyZoneSpec.isTestFunc = isTestFunc;
750 return proxyZone.run(testBody, null, arguments);
751 };
752 // Update the length of wrappedFunc to be the same as the length of the testBody
753 // So jest core can handle whether the test function has `done()` or not correctly
754 Object.defineProperty(wrappedFunc, 'length', { configurable: true, writable: true, enumerable: false });
755 wrappedFunc.length = testBody.length;
756 return wrappedFunc;
757 }
758 ['describe', 'xdescribe', 'fdescribe'].forEach(methodName => {
759 let originalJestFn = context[methodName];
760 if (context[Zone.__symbol__(methodName)]) {
761 return;
762 }
763 context[Zone.__symbol__(methodName)] = originalJestFn;
764 context[methodName] = function (...args) {
765 args[1] = wrapDescribeInZone(args[1]);
766 return originalJestFn.apply(this, args);
767 };
768 context[methodName].each = wrapDescribeFactoryInZone(originalJestFn.each);
769 });
770 context.describe.only = context.fdescribe;
771 context.describe.skip = context.xdescribe;
772 ['it', 'xit', 'fit', 'test', 'xtest'].forEach(methodName => {
773 let originalJestFn = context[methodName];
774 if (context[Zone.__symbol__(methodName)]) {
775 return;
776 }
777 context[Zone.__symbol__(methodName)] = originalJestFn;
778 context[methodName] = function (...args) {
779 args[1] = wrapTestInZone(args[1], true);
780 return originalJestFn.apply(this, args);
781 };
782 context[methodName].each = wrapTestFactoryInZone(originalJestFn.each);
783 context[methodName].todo = originalJestFn.todo;
784 });
785 context.it.only = context.fit;
786 context.it.skip = context.xit;
787 context.test.only = context.fit;
788 context.test.skip = context.xit;
789 ['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(methodName => {
790 let originalJestFn = context[methodName];
791 if (context[Zone.__symbol__(methodName)]) {
792 return;
793 }
794 context[Zone.__symbol__(methodName)] = originalJestFn;
795 context[methodName] = function (...args) {
796 args[0] = wrapTestInZone(args[0]);
797 return originalJestFn.apply(this, args);
798 };
799 });
800 Zone.patchJestObject = function patchJestObject(Timer, isModern = false) {
801 // check whether currently the test is inside fakeAsync()
802 function isPatchingFakeTimer() {
803 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
804 return !!fakeAsyncZoneSpec;
805 }
806 // check whether the current function is inside `test/it` or other methods
807 // such as `describe/beforeEach`
808 function isInTestFunc() {
809 const proxyZoneSpec = Zone.current.get('ProxyZoneSpec');
810 return proxyZoneSpec && proxyZoneSpec.isTestFunc;
811 }
812 if (Timer[api.symbol('fakeTimers')]) {
813 return;
814 }
815 Timer[api.symbol('fakeTimers')] = true;
816 // patch jest fakeTimer internal method to make sure no console.warn print out
817 api.patchMethod(Timer, '_checkFakeTimers', delegate => {
818 return function (self, args) {
819 if (isPatchingFakeTimer()) {
820 return true;
821 }
822 else {
823 return delegate.apply(self, args);
824 }
825 };
826 });
827 // patch useFakeTimers(), set useFakeTimersCalled flag, and make test auto run into fakeAsync
828 api.patchMethod(Timer, 'useFakeTimers', delegate => {
829 return function (self, args) {
830 Zone[api.symbol('useFakeTimersCalled')] = true;
831 if (isModern || isInTestFunc()) {
832 return delegate.apply(self, args);
833 }
834 return self;
835 };
836 });
837 // patch useRealTimers(), unset useFakeTimers flag
838 api.patchMethod(Timer, 'useRealTimers', delegate => {
839 return function (self, args) {
840 Zone[api.symbol('useFakeTimersCalled')] = false;
841 if (isModern || isInTestFunc()) {
842 return delegate.apply(self, args);
843 }
844 return self;
845 };
846 });
847 // patch setSystemTime(), call setCurrentRealTime() in the fakeAsyncTest
848 api.patchMethod(Timer, 'setSystemTime', delegate => {
849 return function (self, args) {
850 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
851 if (fakeAsyncZoneSpec && isPatchingFakeTimer()) {
852 fakeAsyncZoneSpec.setFakeBaseSystemTime(args[0]);
853 }
854 else {
855 return delegate.apply(self, args);
856 }
857 };
858 });
859 // patch getSystemTime(), call getCurrentRealTime() in the fakeAsyncTest
860 api.patchMethod(Timer, 'getRealSystemTime', delegate => {
861 return function (self, args) {
862 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
863 if (fakeAsyncZoneSpec && isPatchingFakeTimer()) {
864 return fakeAsyncZoneSpec.getRealSystemTime();
865 }
866 else {
867 return delegate.apply(self, args);
868 }
869 };
870 });
871 // patch runAllTicks(), run all microTasks inside fakeAsync
872 api.patchMethod(Timer, 'runAllTicks', delegate => {
873 return function (self, args) {
874 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
875 if (fakeAsyncZoneSpec) {
876 fakeAsyncZoneSpec.flushMicrotasks();
877 }
878 else {
879 return delegate.apply(self, args);
880 }
881 };
882 });
883 // patch runAllTimers(), run all macroTasks inside fakeAsync
884 api.patchMethod(Timer, 'runAllTimers', delegate => {
885 return function (self, args) {
886 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
887 if (fakeAsyncZoneSpec) {
888 fakeAsyncZoneSpec.flush(100, true);
889 }
890 else {
891 return delegate.apply(self, args);
892 }
893 };
894 });
895 // patch advanceTimersByTime(), call tick() in the fakeAsyncTest
896 api.patchMethod(Timer, 'advanceTimersByTime', delegate => {
897 return function (self, args) {
898 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
899 if (fakeAsyncZoneSpec) {
900 fakeAsyncZoneSpec.tick(args[0]);
901 }
902 else {
903 return delegate.apply(self, args);
904 }
905 };
906 });
907 // patch runOnlyPendingTimers(), call flushOnlyPendingTimers() in the fakeAsyncTest
908 api.patchMethod(Timer, 'runOnlyPendingTimers', delegate => {
909 return function (self, args) {
910 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
911 if (fakeAsyncZoneSpec) {
912 fakeAsyncZoneSpec.flushOnlyPendingTimers();
913 }
914 else {
915 return delegate.apply(self, args);
916 }
917 };
918 });
919 // patch advanceTimersToNextTimer(), call tickToNext() in the fakeAsyncTest
920 api.patchMethod(Timer, 'advanceTimersToNextTimer', delegate => {
921 return function (self, args) {
922 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
923 if (fakeAsyncZoneSpec) {
924 fakeAsyncZoneSpec.tickToNext(args[0]);
925 }
926 else {
927 return delegate.apply(self, args);
928 }
929 };
930 });
931 // patch clearAllTimers(), call removeAllTimers() in the fakeAsyncTest
932 api.patchMethod(Timer, 'clearAllTimers', delegate => {
933 return function (self, args) {
934 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
935 if (fakeAsyncZoneSpec) {
936 fakeAsyncZoneSpec.removeAllTimers();
937 }
938 else {
939 return delegate.apply(self, args);
940 }
941 };
942 });
943 // patch getTimerCount(), call getTimerCount() in the fakeAsyncTest
944 api.patchMethod(Timer, 'getTimerCount', delegate => {
945 return function (self, args) {
946 const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
947 if (fakeAsyncZoneSpec) {
948 return fakeAsyncZoneSpec.getTimerCount();
949 }
950 else {
951 return delegate.apply(self, args);
952 }
953 };
954 });
955 };
956});
957
958/**
959 * @license
960 * Copyright Google LLC All Rights Reserved.
961 *
962 * Use of this source code is governed by an MIT-style license that can be
963 * found in the LICENSE file at https://angular.io/license
964 */
965Zone.__load_patch('mocha', (global, Zone) => {
966 const Mocha = global.Mocha;
967 if (typeof Mocha === 'undefined') {
968 // return if Mocha is not available, because now zone-testing
969 // will load mocha patch with jasmine/jest patch
970 return;
971 }
972 if (typeof Zone === 'undefined') {
973 throw new Error('Missing Zone.js');
974 }
975 const ProxyZoneSpec = Zone['ProxyZoneSpec'];
976 const SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
977 if (!ProxyZoneSpec) {
978 throw new Error('Missing ProxyZoneSpec');
979 }
980 if (Mocha['__zone_patch__']) {
981 throw new Error('"Mocha" has already been patched with "Zone".');
982 }
983 Mocha['__zone_patch__'] = true;
984 const rootZone = Zone.current;
985 const syncZone = rootZone.fork(new SyncTestZoneSpec('Mocha.describe'));
986 let testZone = null;
987 const suiteZone = rootZone.fork(new ProxyZoneSpec());
988 const mochaOriginal = {
989 after: Mocha.after,
990 afterEach: Mocha.afterEach,
991 before: Mocha.before,
992 beforeEach: Mocha.beforeEach,
993 describe: Mocha.describe,
994 it: Mocha.it
995 };
996 function modifyArguments(args, syncTest, asyncTest) {
997 for (let i = 0; i < args.length; i++) {
998 let arg = args[i];
999 if (typeof arg === 'function') {
1000 // The `done` callback is only passed through if the function expects at
1001 // least one argument.
1002 // Note we have to make a function with correct number of arguments,
1003 // otherwise mocha will
1004 // think that all functions are sync or async.
1005 args[i] = (arg.length === 0) ? syncTest(arg) : asyncTest(arg);
1006 // Mocha uses toString to view the test body in the result list, make sure we return the
1007 // correct function body
1008 args[i].toString = function () {
1009 return arg.toString();
1010 };
1011 }
1012 }
1013 return args;
1014 }
1015 function wrapDescribeInZone(args) {
1016 const syncTest = function (fn) {
1017 return function () {
1018 return syncZone.run(fn, this, arguments);
1019 };
1020 };
1021 return modifyArguments(args, syncTest);
1022 }
1023 function wrapTestInZone(args) {
1024 const asyncTest = function (fn) {
1025 return function (done) {
1026 return testZone.run(fn, this, [done]);
1027 };
1028 };
1029 const syncTest = function (fn) {
1030 return function () {
1031 return testZone.run(fn, this);
1032 };
1033 };
1034 return modifyArguments(args, syncTest, asyncTest);
1035 }
1036 function wrapSuiteInZone(args) {
1037 const asyncTest = function (fn) {
1038 return function (done) {
1039 return suiteZone.run(fn, this, [done]);
1040 };
1041 };
1042 const syncTest = function (fn) {
1043 return function () {
1044 return suiteZone.run(fn, this);
1045 };
1046 };
1047 return modifyArguments(args, syncTest, asyncTest);
1048 }
1049 global.describe = global.suite = Mocha.describe = function () {
1050 return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments));
1051 };
1052 global.xdescribe = global.suite.skip = Mocha.describe.skip = function () {
1053 return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments));
1054 };
1055 global.describe.only = global.suite.only = Mocha.describe.only = function () {
1056 return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments));
1057 };
1058 global.it = global.specify = global.test = Mocha.it = function () {
1059 return mochaOriginal.it.apply(this, wrapTestInZone(arguments));
1060 };
1061 global.xit = global.xspecify = Mocha.it.skip = function () {
1062 return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments));
1063 };
1064 global.it.only = global.test.only = Mocha.it.only = function () {
1065 return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments));
1066 };
1067 global.after = global.suiteTeardown = Mocha.after = function () {
1068 return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments));
1069 };
1070 global.afterEach = global.teardown = Mocha.afterEach = function () {
1071 return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments));
1072 };
1073 global.before = global.suiteSetup = Mocha.before = function () {
1074 return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments));
1075 };
1076 global.beforeEach = global.setup = Mocha.beforeEach = function () {
1077 return mochaOriginal.beforeEach.apply(this, wrapTestInZone(arguments));
1078 };
1079 ((originalRunTest, originalRun) => {
1080 Mocha.Runner.prototype.runTest = function (fn) {
1081 Zone.current.scheduleMicroTask('mocha.forceTask', () => {
1082 originalRunTest.call(this, fn);
1083 });
1084 };
1085 Mocha.Runner.prototype.run = function (fn) {
1086 this.on('test', (e) => {
1087 testZone = rootZone.fork(new ProxyZoneSpec());
1088 });
1089 this.on('fail', (test, err) => {
1090 const proxyZoneSpec = testZone && testZone.get('ProxyZoneSpec');
1091 if (proxyZoneSpec && err) {
1092 try {
1093 // try catch here in case err.message is not writable
1094 err.message += proxyZoneSpec.getAndClearPendingTasksInfo();
1095 }
1096 catch (error) {
1097 }
1098 }
1099 });
1100 return originalRun.call(this, fn);
1101 };
1102 })(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run);
1103});
1104
1105/**
1106 * @license
1107 * Copyright Google LLC All Rights Reserved.
1108 *
1109 * Use of this source code is governed by an MIT-style license that can be
1110 * found in the LICENSE file at https://angular.io/license
1111 */
1112(function (_global) {
1113 class AsyncTestZoneSpec {
1114 constructor(finishCallback, failCallback, namePrefix) {
1115 this.finishCallback = finishCallback;
1116 this.failCallback = failCallback;
1117 this._pendingMicroTasks = false;
1118 this._pendingMacroTasks = false;
1119 this._alreadyErrored = false;
1120 this._isSync = false;
1121 this.runZone = Zone.current;
1122 this.unresolvedChainedPromiseCount = 0;
1123 this.supportWaitUnresolvedChainedPromise = false;
1124 this.name = 'asyncTestZone for ' + namePrefix;
1125 this.properties = { 'AsyncTestZoneSpec': this };
1126 this.supportWaitUnresolvedChainedPromise =
1127 _global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] === true;
1128 }
1129 isUnresolvedChainedPromisePending() {
1130 return this.unresolvedChainedPromiseCount > 0;
1131 }
1132 _finishCallbackIfDone() {
1133 if (!(this._pendingMicroTasks || this._pendingMacroTasks ||
1134 (this.supportWaitUnresolvedChainedPromise && this.isUnresolvedChainedPromisePending()))) {
1135 // We do this because we would like to catch unhandled rejected promises.
1136 this.runZone.run(() => {
1137 setTimeout(() => {
1138 if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) {
1139 this.finishCallback();
1140 }
1141 }, 0);
1142 });
1143 }
1144 }
1145 patchPromiseForTest() {
1146 if (!this.supportWaitUnresolvedChainedPromise) {
1147 return;
1148 }
1149 const patchPromiseForTest = Promise[Zone.__symbol__('patchPromiseForTest')];
1150 if (patchPromiseForTest) {
1151 patchPromiseForTest();
1152 }
1153 }
1154 unPatchPromiseForTest() {
1155 if (!this.supportWaitUnresolvedChainedPromise) {
1156 return;
1157 }
1158 const unPatchPromiseForTest = Promise[Zone.__symbol__('unPatchPromiseForTest')];
1159 if (unPatchPromiseForTest) {
1160 unPatchPromiseForTest();
1161 }
1162 }
1163 onScheduleTask(delegate, current, target, task) {
1164 if (task.type !== 'eventTask') {
1165 this._isSync = false;
1166 }
1167 if (task.type === 'microTask' && task.data && task.data instanceof Promise) {
1168 // check whether the promise is a chained promise
1169 if (task.data[AsyncTestZoneSpec.symbolParentUnresolved] === true) {
1170 // chained promise is being scheduled
1171 this.unresolvedChainedPromiseCount--;
1172 }
1173 }
1174 return delegate.scheduleTask(target, task);
1175 }
1176 onInvokeTask(delegate, current, target, task, applyThis, applyArgs) {
1177 if (task.type !== 'eventTask') {
1178 this._isSync = false;
1179 }
1180 return delegate.invokeTask(target, task, applyThis, applyArgs);
1181 }
1182 onCancelTask(delegate, current, target, task) {
1183 if (task.type !== 'eventTask') {
1184 this._isSync = false;
1185 }
1186 return delegate.cancelTask(target, task);
1187 }
1188 // Note - we need to use onInvoke at the moment to call finish when a test is
1189 // fully synchronous. TODO(juliemr): remove this when the logic for
1190 // onHasTask changes and it calls whenever the task queues are dirty.
1191 // updated by(JiaLiPassion), only call finish callback when no task
1192 // was scheduled/invoked/canceled.
1193 onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
1194 try {
1195 this._isSync = true;
1196 return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
1197 }
1198 finally {
1199 const afterTaskCounts = parentZoneDelegate._taskCounts;
1200 if (this._isSync) {
1201 this._finishCallbackIfDone();
1202 }
1203 }
1204 }
1205 onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
1206 // Let the parent try to handle the error.
1207 const result = parentZoneDelegate.handleError(targetZone, error);
1208 if (result) {
1209 this.failCallback(error);
1210 this._alreadyErrored = true;
1211 }
1212 return false;
1213 }
1214 onHasTask(delegate, current, target, hasTaskState) {
1215 delegate.hasTask(target, hasTaskState);
1216 if (hasTaskState.change == 'microTask') {
1217 this._pendingMicroTasks = hasTaskState.microTask;
1218 this._finishCallbackIfDone();
1219 }
1220 else if (hasTaskState.change == 'macroTask') {
1221 this._pendingMacroTasks = hasTaskState.macroTask;
1222 this._finishCallbackIfDone();
1223 }
1224 }
1225 }
1226 AsyncTestZoneSpec.symbolParentUnresolved = Zone.__symbol__('parentUnresolved');
1227 // Export the class so that new instances can be created with proper
1228 // constructor params.
1229 Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
1230})(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global);
1231Zone.__load_patch('asynctest', (global, Zone, api) => {
1232 /**
1233 * Wraps a test function in an asynchronous test zone. The test will automatically
1234 * complete when all asynchronous calls within this zone are done.
1235 */
1236 Zone[api.symbol('asyncTest')] = function asyncTest(fn) {
1237 // If we're running using the Jasmine test framework, adapt to call the 'done'
1238 // function when asynchronous activity is finished.
1239 if (global.jasmine) {
1240 // Not using an arrow function to preserve context passed from call site
1241 return function (done) {
1242 if (!done) {
1243 // if we run beforeEach in @angular/core/testing/testing_internal then we get no done
1244 // fake it here and assume sync.
1245 done = function () { };
1246 done.fail = function (e) {
1247 throw e;
1248 };
1249 }
1250 runInTestZone(fn, this, done, (err) => {
1251 if (typeof err === 'string') {
1252 return done.fail(new Error(err));
1253 }
1254 else {
1255 done.fail(err);
1256 }
1257 });
1258 };
1259 }
1260 // Otherwise, return a promise which will resolve when asynchronous activity
1261 // is finished. This will be correctly consumed by the Mocha framework with
1262 // it('...', async(myFn)); or can be used in a custom framework.
1263 // Not using an arrow function to preserve context passed from call site
1264 return function () {
1265 return new Promise((finishCallback, failCallback) => {
1266 runInTestZone(fn, this, finishCallback, failCallback);
1267 });
1268 };
1269 };
1270 function runInTestZone(fn, context, finishCallback, failCallback) {
1271 const currentZone = Zone.current;
1272 const AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
1273 if (AsyncTestZoneSpec === undefined) {
1274 throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
1275 'Please make sure that your environment includes zone.js/dist/async-test.js');
1276 }
1277 const ProxyZoneSpec = Zone['ProxyZoneSpec'];
1278 if (!ProxyZoneSpec) {
1279 throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
1280 'Please make sure that your environment includes zone.js/dist/proxy.js');
1281 }
1282 const proxyZoneSpec = ProxyZoneSpec.get();
1283 ProxyZoneSpec.assertPresent();
1284 // We need to create the AsyncTestZoneSpec outside the ProxyZone.
1285 // If we do it in ProxyZone then we will get to infinite recursion.
1286 const proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
1287 const previousDelegate = proxyZoneSpec.getDelegate();
1288 proxyZone.parent.run(() => {
1289 const testZoneSpec = new AsyncTestZoneSpec(() => {
1290 // Need to restore the original zone.
1291 if (proxyZoneSpec.getDelegate() == testZoneSpec) {
1292 // Only reset the zone spec if it's
1293 // sill this one. Otherwise, assume
1294 // it's OK.
1295 proxyZoneSpec.setDelegate(previousDelegate);
1296 }
1297 testZoneSpec.unPatchPromiseForTest();
1298 currentZone.run(() => {
1299 finishCallback();
1300 });
1301 }, (error) => {
1302 // Need to restore the original zone.
1303 if (proxyZoneSpec.getDelegate() == testZoneSpec) {
1304 // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
1305 proxyZoneSpec.setDelegate(previousDelegate);
1306 }
1307 testZoneSpec.unPatchPromiseForTest();
1308 currentZone.run(() => {
1309 failCallback(error);
1310 });
1311 }, 'test');
1312 proxyZoneSpec.setDelegate(testZoneSpec);
1313 testZoneSpec.patchPromiseForTest();
1314 });
1315 return Zone.current.runGuarded(fn, context);
1316 }
1317});
1318
1319/**
1320 * @license
1321 * Copyright Google LLC All Rights Reserved.
1322 *
1323 * Use of this source code is governed by an MIT-style license that can be
1324 * found in the LICENSE file at https://angular.io/license
1325 */
1326(function (global) {
1327 const OriginalDate = global.Date;
1328 // Since when we compile this file to `es2015`, and if we define
1329 // this `FakeDate` as `class FakeDate`, and then set `FakeDate.prototype`
1330 // there will be an error which is `Cannot assign to read only property 'prototype'`
1331 // so we need to use function implementation here.
1332 function FakeDate() {
1333 if (arguments.length === 0) {
1334 const d = new OriginalDate();
1335 d.setTime(FakeDate.now());
1336 return d;
1337 }
1338 else {
1339 const args = Array.prototype.slice.call(arguments);
1340 return new OriginalDate(...args);
1341 }
1342 }
1343 FakeDate.now = function () {
1344 const fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
1345 if (fakeAsyncTestZoneSpec) {
1346 return fakeAsyncTestZoneSpec.getFakeSystemTime();
1347 }
1348 return OriginalDate.now.apply(this, arguments);
1349 };
1350 FakeDate.UTC = OriginalDate.UTC;
1351 FakeDate.parse = OriginalDate.parse;
1352 // keep a reference for zone patched timer function
1353 const timers = {
1354 setTimeout: global.setTimeout,
1355 setInterval: global.setInterval,
1356 clearTimeout: global.clearTimeout,
1357 clearInterval: global.clearInterval
1358 };
1359 class Scheduler {
1360 constructor() {
1361 // Scheduler queue with the tuple of end time and callback function - sorted by end time.
1362 this._schedulerQueue = [];
1363 // Current simulated time in millis.
1364 this._currentTickTime = 0;
1365 // Current fake system base time in millis.
1366 this._currentFakeBaseSystemTime = OriginalDate.now();
1367 // track requeuePeriodicTimer
1368 this._currentTickRequeuePeriodicEntries = [];
1369 }
1370 getCurrentTickTime() {
1371 return this._currentTickTime;
1372 }
1373 getFakeSystemTime() {
1374 return this._currentFakeBaseSystemTime + this._currentTickTime;
1375 }
1376 setFakeBaseSystemTime(fakeBaseSystemTime) {
1377 this._currentFakeBaseSystemTime = fakeBaseSystemTime;
1378 }
1379 getRealSystemTime() {
1380 return OriginalDate.now();
1381 }
1382 scheduleFunction(cb, delay, options) {
1383 options = Object.assign({
1384 args: [],
1385 isPeriodic: false,
1386 isRequestAnimationFrame: false,
1387 id: -1,
1388 isRequeuePeriodic: false
1389 }, options);
1390 let currentId = options.id < 0 ? Scheduler.nextId++ : options.id;
1391 let endTime = this._currentTickTime + delay;
1392 // Insert so that scheduler queue remains sorted by end time.
1393 let newEntry = {
1394 endTime: endTime,
1395 id: currentId,
1396 func: cb,
1397 args: options.args,
1398 delay: delay,
1399 isPeriodic: options.isPeriodic,
1400 isRequestAnimationFrame: options.isRequestAnimationFrame
1401 };
1402 if (options.isRequeuePeriodic) {
1403 this._currentTickRequeuePeriodicEntries.push(newEntry);
1404 }
1405 let i = 0;
1406 for (; i < this._schedulerQueue.length; i++) {
1407 let currentEntry = this._schedulerQueue[i];
1408 if (newEntry.endTime < currentEntry.endTime) {
1409 break;
1410 }
1411 }
1412 this._schedulerQueue.splice(i, 0, newEntry);
1413 return currentId;
1414 }
1415 removeScheduledFunctionWithId(id) {
1416 for (let i = 0; i < this._schedulerQueue.length; i++) {
1417 if (this._schedulerQueue[i].id == id) {
1418 this._schedulerQueue.splice(i, 1);
1419 break;
1420 }
1421 }
1422 }
1423 removeAll() {
1424 this._schedulerQueue = [];
1425 }
1426 getTimerCount() {
1427 return this._schedulerQueue.length;
1428 }
1429 tickToNext(step = 1, doTick, tickOptions) {
1430 if (this._schedulerQueue.length < step) {
1431 return;
1432 }
1433 // Find the last task currently queued in the scheduler queue and tick
1434 // till that time.
1435 const startTime = this._currentTickTime;
1436 const targetTask = this._schedulerQueue[step - 1];
1437 this.tick(targetTask.endTime - startTime, doTick, tickOptions);
1438 }
1439 tick(millis = 0, doTick, tickOptions) {
1440 let finalTime = this._currentTickTime + millis;
1441 let lastCurrentTime = 0;
1442 tickOptions = Object.assign({ processNewMacroTasksSynchronously: true }, tickOptions);
1443 // we need to copy the schedulerQueue so nested timeout
1444 // will not be wrongly called in the current tick
1445 // https://github.com/angular/angular/issues/33799
1446 const schedulerQueue = tickOptions.processNewMacroTasksSynchronously ?
1447 this._schedulerQueue :
1448 this._schedulerQueue.slice();
1449 if (schedulerQueue.length === 0 && doTick) {
1450 doTick(millis);
1451 return;
1452 }
1453 while (schedulerQueue.length > 0) {
1454 // clear requeueEntries before each loop
1455 this._currentTickRequeuePeriodicEntries = [];
1456 let current = schedulerQueue[0];
1457 if (finalTime < current.endTime) {
1458 // Done processing the queue since it's sorted by endTime.
1459 break;
1460 }
1461 else {
1462 // Time to run scheduled function. Remove it from the head of queue.
1463 let current = schedulerQueue.shift();
1464 if (!tickOptions.processNewMacroTasksSynchronously) {
1465 const idx = this._schedulerQueue.indexOf(current);
1466 if (idx >= 0) {
1467 this._schedulerQueue.splice(idx, 1);
1468 }
1469 }
1470 lastCurrentTime = this._currentTickTime;
1471 this._currentTickTime = current.endTime;
1472 if (doTick) {
1473 doTick(this._currentTickTime - lastCurrentTime);
1474 }
1475 let retval = current.func.apply(global, current.isRequestAnimationFrame ? [this._currentTickTime] : current.args);
1476 if (!retval) {
1477 // Uncaught exception in the current scheduled function. Stop processing the queue.
1478 break;
1479 }
1480 // check is there any requeue periodic entry is added in
1481 // current loop, if there is, we need to add to current loop
1482 if (!tickOptions.processNewMacroTasksSynchronously) {
1483 this._currentTickRequeuePeriodicEntries.forEach(newEntry => {
1484 let i = 0;
1485 for (; i < schedulerQueue.length; i++) {
1486 const currentEntry = schedulerQueue[i];
1487 if (newEntry.endTime < currentEntry.endTime) {
1488 break;
1489 }
1490 }
1491 schedulerQueue.splice(i, 0, newEntry);
1492 });
1493 }
1494 }
1495 }
1496 lastCurrentTime = this._currentTickTime;
1497 this._currentTickTime = finalTime;
1498 if (doTick) {
1499 doTick(this._currentTickTime - lastCurrentTime);
1500 }
1501 }
1502 flushOnlyPendingTimers(doTick) {
1503 if (this._schedulerQueue.length === 0) {
1504 return 0;
1505 }
1506 // Find the last task currently queued in the scheduler queue and tick
1507 // till that time.
1508 const startTime = this._currentTickTime;
1509 const lastTask = this._schedulerQueue[this._schedulerQueue.length - 1];
1510 this.tick(lastTask.endTime - startTime, doTick, { processNewMacroTasksSynchronously: false });
1511 return this._currentTickTime - startTime;
1512 }
1513 flush(limit = 20, flushPeriodic = false, doTick) {
1514 if (flushPeriodic) {
1515 return this.flushPeriodic(doTick);
1516 }
1517 else {
1518 return this.flushNonPeriodic(limit, doTick);
1519 }
1520 }
1521 flushPeriodic(doTick) {
1522 if (this._schedulerQueue.length === 0) {
1523 return 0;
1524 }
1525 // Find the last task currently queued in the scheduler queue and tick
1526 // till that time.
1527 const startTime = this._currentTickTime;
1528 const lastTask = this._schedulerQueue[this._schedulerQueue.length - 1];
1529 this.tick(lastTask.endTime - startTime, doTick);
1530 return this._currentTickTime - startTime;
1531 }
1532 flushNonPeriodic(limit, doTick) {
1533 const startTime = this._currentTickTime;
1534 let lastCurrentTime = 0;
1535 let count = 0;
1536 while (this._schedulerQueue.length > 0) {
1537 count++;
1538 if (count > limit) {
1539 throw new Error('flush failed after reaching the limit of ' + limit +
1540 ' tasks. Does your code use a polling timeout?');
1541 }
1542 // flush only non-periodic timers.
1543 // If the only remaining tasks are periodic(or requestAnimationFrame), finish flushing.
1544 if (this._schedulerQueue.filter(task => !task.isPeriodic && !task.isRequestAnimationFrame)
1545 .length === 0) {
1546 break;
1547 }
1548 const current = this._schedulerQueue.shift();
1549 lastCurrentTime = this._currentTickTime;
1550 this._currentTickTime = current.endTime;
1551 if (doTick) {
1552 // Update any secondary schedulers like Jasmine mock Date.
1553 doTick(this._currentTickTime - lastCurrentTime);
1554 }
1555 const retval = current.func.apply(global, current.args);
1556 if (!retval) {
1557 // Uncaught exception in the current scheduled function. Stop processing the queue.
1558 break;
1559 }
1560 }
1561 return this._currentTickTime - startTime;
1562 }
1563 }
1564 // Next scheduler id.
1565 Scheduler.nextId = 1;
1566 class FakeAsyncTestZoneSpec {
1567 constructor(namePrefix, trackPendingRequestAnimationFrame = false, macroTaskOptions) {
1568 this.trackPendingRequestAnimationFrame = trackPendingRequestAnimationFrame;
1569 this.macroTaskOptions = macroTaskOptions;
1570 this._scheduler = new Scheduler();
1571 this._microtasks = [];
1572 this._lastError = null;
1573 this._uncaughtPromiseErrors = Promise[Zone.__symbol__('uncaughtPromiseErrors')];
1574 this.pendingPeriodicTimers = [];
1575 this.pendingTimers = [];
1576 this.patchDateLocked = false;
1577 this.properties = { 'FakeAsyncTestZoneSpec': this };
1578 this.name = 'fakeAsyncTestZone for ' + namePrefix;
1579 // in case user can't access the construction of FakeAsyncTestSpec
1580 // user can also define macroTaskOptions by define a global variable.
1581 if (!this.macroTaskOptions) {
1582 this.macroTaskOptions = global[Zone.__symbol__('FakeAsyncTestMacroTask')];
1583 }
1584 }
1585 static assertInZone() {
1586 if (Zone.current.get('FakeAsyncTestZoneSpec') == null) {
1587 throw new Error('The code should be running in the fakeAsync zone to call this function');
1588 }
1589 }
1590 _fnAndFlush(fn, completers) {
1591 return (...args) => {
1592 fn.apply(global, args);
1593 if (this._lastError === null) { // Success
1594 if (completers.onSuccess != null) {
1595 completers.onSuccess.apply(global);
1596 }
1597 // Flush microtasks only on success.
1598 this.flushMicrotasks();
1599 }
1600 else { // Failure
1601 if (completers.onError != null) {
1602 completers.onError.apply(global);
1603 }
1604 }
1605 // Return true if there were no errors, false otherwise.
1606 return this._lastError === null;
1607 };
1608 }
1609 static _removeTimer(timers, id) {
1610 let index = timers.indexOf(id);
1611 if (index > -1) {
1612 timers.splice(index, 1);
1613 }
1614 }
1615 _dequeueTimer(id) {
1616 return () => {
1617 FakeAsyncTestZoneSpec._removeTimer(this.pendingTimers, id);
1618 };
1619 }
1620 _requeuePeriodicTimer(fn, interval, args, id) {
1621 return () => {
1622 // Requeue the timer callback if it's not been canceled.
1623 if (this.pendingPeriodicTimers.indexOf(id) !== -1) {
1624 this._scheduler.scheduleFunction(fn, interval, { args, isPeriodic: true, id, isRequeuePeriodic: true });
1625 }
1626 };
1627 }
1628 _dequeuePeriodicTimer(id) {
1629 return () => {
1630 FakeAsyncTestZoneSpec._removeTimer(this.pendingPeriodicTimers, id);
1631 };
1632 }
1633 _setTimeout(fn, delay, args, isTimer = true) {
1634 let removeTimerFn = this._dequeueTimer(Scheduler.nextId);
1635 // Queue the callback and dequeue the timer on success and error.
1636 let cb = this._fnAndFlush(fn, { onSuccess: removeTimerFn, onError: removeTimerFn });
1637 let id = this._scheduler.scheduleFunction(cb, delay, { args, isRequestAnimationFrame: !isTimer });
1638 if (isTimer) {
1639 this.pendingTimers.push(id);
1640 }
1641 return id;
1642 }
1643 _clearTimeout(id) {
1644 FakeAsyncTestZoneSpec._removeTimer(this.pendingTimers, id);
1645 this._scheduler.removeScheduledFunctionWithId(id);
1646 }
1647 _setInterval(fn, interval, args) {
1648 let id = Scheduler.nextId;
1649 let completers = { onSuccess: null, onError: this._dequeuePeriodicTimer(id) };
1650 let cb = this._fnAndFlush(fn, completers);
1651 // Use the callback created above to requeue on success.
1652 completers.onSuccess = this._requeuePeriodicTimer(cb, interval, args, id);
1653 // Queue the callback and dequeue the periodic timer only on error.
1654 this._scheduler.scheduleFunction(cb, interval, { args, isPeriodic: true });
1655 this.pendingPeriodicTimers.push(id);
1656 return id;
1657 }
1658 _clearInterval(id) {
1659 FakeAsyncTestZoneSpec._removeTimer(this.pendingPeriodicTimers, id);
1660 this._scheduler.removeScheduledFunctionWithId(id);
1661 }
1662 _resetLastErrorAndThrow() {
1663 let error = this._lastError || this._uncaughtPromiseErrors[0];
1664 this._uncaughtPromiseErrors.length = 0;
1665 this._lastError = null;
1666 throw error;
1667 }
1668 getCurrentTickTime() {
1669 return this._scheduler.getCurrentTickTime();
1670 }
1671 getFakeSystemTime() {
1672 return this._scheduler.getFakeSystemTime();
1673 }
1674 setFakeBaseSystemTime(realTime) {
1675 this._scheduler.setFakeBaseSystemTime(realTime);
1676 }
1677 getRealSystemTime() {
1678 return this._scheduler.getRealSystemTime();
1679 }
1680 static patchDate() {
1681 if (!!global[Zone.__symbol__('disableDatePatching')]) {
1682 // we don't want to patch global Date
1683 // because in some case, global Date
1684 // is already being patched, we need to provide
1685 // an option to let user still use their
1686 // own version of Date.
1687 return;
1688 }
1689 if (global['Date'] === FakeDate) {
1690 // already patched
1691 return;
1692 }
1693 global['Date'] = FakeDate;
1694 FakeDate.prototype = OriginalDate.prototype;
1695 // try check and reset timers
1696 // because jasmine.clock().install() may
1697 // have replaced the global timer
1698 FakeAsyncTestZoneSpec.checkTimerPatch();
1699 }
1700 static resetDate() {
1701 if (global['Date'] === FakeDate) {
1702 global['Date'] = OriginalDate;
1703 }
1704 }
1705 static checkTimerPatch() {
1706 if (global.setTimeout !== timers.setTimeout) {
1707 global.setTimeout = timers.setTimeout;
1708 global.clearTimeout = timers.clearTimeout;
1709 }
1710 if (global.setInterval !== timers.setInterval) {
1711 global.setInterval = timers.setInterval;
1712 global.clearInterval = timers.clearInterval;
1713 }
1714 }
1715 lockDatePatch() {
1716 this.patchDateLocked = true;
1717 FakeAsyncTestZoneSpec.patchDate();
1718 }
1719 unlockDatePatch() {
1720 this.patchDateLocked = false;
1721 FakeAsyncTestZoneSpec.resetDate();
1722 }
1723 tickToNext(steps = 1, doTick, tickOptions = { processNewMacroTasksSynchronously: true }) {
1724 if (steps <= 0) {
1725 return;
1726 }
1727 FakeAsyncTestZoneSpec.assertInZone();
1728 this.flushMicrotasks();
1729 this._scheduler.tickToNext(steps, doTick, tickOptions);
1730 if (this._lastError !== null) {
1731 this._resetLastErrorAndThrow();
1732 }
1733 }
1734 tick(millis = 0, doTick, tickOptions = { processNewMacroTasksSynchronously: true }) {
1735 FakeAsyncTestZoneSpec.assertInZone();
1736 this.flushMicrotasks();
1737 this._scheduler.tick(millis, doTick, tickOptions);
1738 if (this._lastError !== null) {
1739 this._resetLastErrorAndThrow();
1740 }
1741 }
1742 flushMicrotasks() {
1743 FakeAsyncTestZoneSpec.assertInZone();
1744 const flushErrors = () => {
1745 if (this._lastError !== null || this._uncaughtPromiseErrors.length) {
1746 // If there is an error stop processing the microtask queue and rethrow the error.
1747 this._resetLastErrorAndThrow();
1748 }
1749 };
1750 while (this._microtasks.length > 0) {
1751 let microtask = this._microtasks.shift();
1752 microtask.func.apply(microtask.target, microtask.args);
1753 }
1754 flushErrors();
1755 }
1756 flush(limit, flushPeriodic, doTick) {
1757 FakeAsyncTestZoneSpec.assertInZone();
1758 this.flushMicrotasks();
1759 const elapsed = this._scheduler.flush(limit, flushPeriodic, doTick);
1760 if (this._lastError !== null) {
1761 this._resetLastErrorAndThrow();
1762 }
1763 return elapsed;
1764 }
1765 flushOnlyPendingTimers(doTick) {
1766 FakeAsyncTestZoneSpec.assertInZone();
1767 this.flushMicrotasks();
1768 const elapsed = this._scheduler.flushOnlyPendingTimers(doTick);
1769 if (this._lastError !== null) {
1770 this._resetLastErrorAndThrow();
1771 }
1772 return elapsed;
1773 }
1774 removeAllTimers() {
1775 FakeAsyncTestZoneSpec.assertInZone();
1776 this._scheduler.removeAll();
1777 this.pendingPeriodicTimers = [];
1778 this.pendingTimers = [];
1779 }
1780 getTimerCount() {
1781 return this._scheduler.getTimerCount() + this._microtasks.length;
1782 }
1783 onScheduleTask(delegate, current, target, task) {
1784 switch (task.type) {
1785 case 'microTask':
1786 let args = task.data && task.data.args;
1787 // should pass additional arguments to callback if have any
1788 // currently we know process.nextTick will have such additional
1789 // arguments
1790 let additionalArgs;
1791 if (args) {
1792 let callbackIndex = task.data.cbIdx;
1793 if (typeof args.length === 'number' && args.length > callbackIndex + 1) {
1794 additionalArgs = Array.prototype.slice.call(args, callbackIndex + 1);
1795 }
1796 }
1797 this._microtasks.push({
1798 func: task.invoke,
1799 args: additionalArgs,
1800 target: task.data && task.data.target
1801 });
1802 break;
1803 case 'macroTask':
1804 switch (task.source) {
1805 case 'setTimeout':
1806 task.data['handleId'] = this._setTimeout(task.invoke, task.data['delay'], Array.prototype.slice.call(task.data['args'], 2));
1807 break;
1808 case 'setImmediate':
1809 task.data['handleId'] = this._setTimeout(task.invoke, 0, Array.prototype.slice.call(task.data['args'], 1));
1810 break;
1811 case 'setInterval':
1812 task.data['handleId'] = this._setInterval(task.invoke, task.data['delay'], Array.prototype.slice.call(task.data['args'], 2));
1813 break;
1814 case 'XMLHttpRequest.send':
1815 throw new Error('Cannot make XHRs from within a fake async test. Request URL: ' +
1816 task.data['url']);
1817 case 'requestAnimationFrame':
1818 case 'webkitRequestAnimationFrame':
1819 case 'mozRequestAnimationFrame':
1820 // Simulate a requestAnimationFrame by using a setTimeout with 16 ms.
1821 // (60 frames per second)
1822 task.data['handleId'] = this._setTimeout(task.invoke, 16, task.data['args'], this.trackPendingRequestAnimationFrame);
1823 break;
1824 default:
1825 // user can define which macroTask they want to support by passing
1826 // macroTaskOptions
1827 const macroTaskOption = this.findMacroTaskOption(task);
1828 if (macroTaskOption) {
1829 const args = task.data && task.data['args'];
1830 const delay = args && args.length > 1 ? args[1] : 0;
1831 let callbackArgs = macroTaskOption.callbackArgs ? macroTaskOption.callbackArgs : args;
1832 if (!!macroTaskOption.isPeriodic) {
1833 // periodic macroTask, use setInterval to simulate
1834 task.data['handleId'] = this._setInterval(task.invoke, delay, callbackArgs);
1835 task.data.isPeriodic = true;
1836 }
1837 else {
1838 // not periodic, use setTimeout to simulate
1839 task.data['handleId'] = this._setTimeout(task.invoke, delay, callbackArgs);
1840 }
1841 break;
1842 }
1843 throw new Error('Unknown macroTask scheduled in fake async test: ' + task.source);
1844 }
1845 break;
1846 case 'eventTask':
1847 task = delegate.scheduleTask(target, task);
1848 break;
1849 }
1850 return task;
1851 }
1852 onCancelTask(delegate, current, target, task) {
1853 switch (task.source) {
1854 case 'setTimeout':
1855 case 'requestAnimationFrame':
1856 case 'webkitRequestAnimationFrame':
1857 case 'mozRequestAnimationFrame':
1858 return this._clearTimeout(task.data['handleId']);
1859 case 'setInterval':
1860 return this._clearInterval(task.data['handleId']);
1861 default:
1862 // user can define which macroTask they want to support by passing
1863 // macroTaskOptions
1864 const macroTaskOption = this.findMacroTaskOption(task);
1865 if (macroTaskOption) {
1866 const handleId = task.data['handleId'];
1867 return macroTaskOption.isPeriodic ? this._clearInterval(handleId) :
1868 this._clearTimeout(handleId);
1869 }
1870 return delegate.cancelTask(target, task);
1871 }
1872 }
1873 onInvoke(delegate, current, target, callback, applyThis, applyArgs, source) {
1874 try {
1875 FakeAsyncTestZoneSpec.patchDate();
1876 return delegate.invoke(target, callback, applyThis, applyArgs, source);
1877 }
1878 finally {
1879 if (!this.patchDateLocked) {
1880 FakeAsyncTestZoneSpec.resetDate();
1881 }
1882 }
1883 }
1884 findMacroTaskOption(task) {
1885 if (!this.macroTaskOptions) {
1886 return null;
1887 }
1888 for (let i = 0; i < this.macroTaskOptions.length; i++) {
1889 const macroTaskOption = this.macroTaskOptions[i];
1890 if (macroTaskOption.source === task.source) {
1891 return macroTaskOption;
1892 }
1893 }
1894 return null;
1895 }
1896 onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
1897 this._lastError = error;
1898 return false; // Don't propagate error to parent zone.
1899 }
1900 }
1901 // Export the class so that new instances can be created with proper
1902 // constructor params.
1903 Zone['FakeAsyncTestZoneSpec'] = FakeAsyncTestZoneSpec;
1904})(typeof window === 'object' && window || typeof self === 'object' && self || global);
1905Zone.__load_patch('fakeasync', (global, Zone, api) => {
1906 const FakeAsyncTestZoneSpec = Zone && Zone['FakeAsyncTestZoneSpec'];
1907 function getProxyZoneSpec() {
1908 return Zone && Zone['ProxyZoneSpec'];
1909 }
1910 let _fakeAsyncTestZoneSpec = null;
1911 /**
1912 * Clears out the shared fake async zone for a test.
1913 * To be called in a global `beforeEach`.
1914 *
1915 * @experimental
1916 */
1917 function resetFakeAsyncZone() {
1918 if (_fakeAsyncTestZoneSpec) {
1919 _fakeAsyncTestZoneSpec.unlockDatePatch();
1920 }
1921 _fakeAsyncTestZoneSpec = null;
1922 // in node.js testing we may not have ProxyZoneSpec in which case there is nothing to reset.
1923 getProxyZoneSpec() && getProxyZoneSpec().assertPresent().resetDelegate();
1924 }
1925 /**
1926 * Wraps a function to be executed in the fakeAsync zone:
1927 * - microtasks are manually executed by calling `flushMicrotasks()`,
1928 * - timers are synchronous, `tick()` simulates the asynchronous passage of time.
1929 *
1930 * If there are any pending timers at the end of the function, an exception will be thrown.
1931 *
1932 * Can be used to wrap inject() calls.
1933 *
1934 * ## Example
1935 *
1936 * {@example core/testing/ts/fake_async.ts region='basic'}
1937 *
1938 * @param fn
1939 * @returns The function wrapped to be executed in the fakeAsync zone
1940 *
1941 * @experimental
1942 */
1943 function fakeAsync(fn) {
1944 // Not using an arrow function to preserve context passed from call site
1945 const fakeAsyncFn = function (...args) {
1946 const ProxyZoneSpec = getProxyZoneSpec();
1947 if (!ProxyZoneSpec) {
1948 throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
1949 'Please make sure that your environment includes zone.js/dist/proxy.js');
1950 }
1951 const proxyZoneSpec = ProxyZoneSpec.assertPresent();
1952 if (Zone.current.get('FakeAsyncTestZoneSpec')) {
1953 throw new Error('fakeAsync() calls can not be nested');
1954 }
1955 try {
1956 // in case jasmine.clock init a fakeAsyncTestZoneSpec
1957 if (!_fakeAsyncTestZoneSpec) {
1958 if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
1959 throw new Error('fakeAsync() calls can not be nested');
1960 }
1961 _fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
1962 }
1963 let res;
1964 const lastProxyZoneSpec = proxyZoneSpec.getDelegate();
1965 proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
1966 _fakeAsyncTestZoneSpec.lockDatePatch();
1967 try {
1968 res = fn.apply(this, args);
1969 flushMicrotasks();
1970 }
1971 finally {
1972 proxyZoneSpec.setDelegate(lastProxyZoneSpec);
1973 }
1974 if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
1975 throw new Error(`${_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` +
1976 `periodic timer(s) still in the queue.`);
1977 }
1978 if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
1979 throw new Error(`${_fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`);
1980 }
1981 return res;
1982 }
1983 finally {
1984 resetFakeAsyncZone();
1985 }
1986 };
1987 fakeAsyncFn.isFakeAsync = true;
1988 return fakeAsyncFn;
1989 }
1990 function _getFakeAsyncZoneSpec() {
1991 if (_fakeAsyncTestZoneSpec == null) {
1992 _fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
1993 if (_fakeAsyncTestZoneSpec == null) {
1994 throw new Error('The code should be running in the fakeAsync zone to call this function');
1995 }
1996 }
1997 return _fakeAsyncTestZoneSpec;
1998 }
1999 /**
2000 * Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
2001 *
2002 * The microtasks queue is drained at the very start of this function and after any timer callback
2003 * has been executed.
2004 *
2005 * ## Example
2006 *
2007 * {@example core/testing/ts/fake_async.ts region='basic'}
2008 *
2009 * @experimental
2010 */
2011 function tick(millis = 0, ignoreNestedTimeout = false) {
2012 _getFakeAsyncZoneSpec().tick(millis, null, ignoreNestedTimeout);
2013 }
2014 /**
2015 * Simulates the asynchronous passage of time for the timers in the fakeAsync zone by
2016 * draining the macrotask queue until it is empty. The returned value is the milliseconds
2017 * of time that would have been elapsed.
2018 *
2019 * @param maxTurns
2020 * @returns The simulated time elapsed, in millis.
2021 *
2022 * @experimental
2023 */
2024 function flush(maxTurns) {
2025 return _getFakeAsyncZoneSpec().flush(maxTurns);
2026 }
2027 /**
2028 * Discard all remaining periodic tasks.
2029 *
2030 * @experimental
2031 */
2032 function discardPeriodicTasks() {
2033 const zoneSpec = _getFakeAsyncZoneSpec();
2034 const pendingTimers = zoneSpec.pendingPeriodicTimers;
2035 zoneSpec.pendingPeriodicTimers.length = 0;
2036 }
2037 /**
2038 * Flush any pending microtasks.
2039 *
2040 * @experimental
2041 */
2042 function flushMicrotasks() {
2043 _getFakeAsyncZoneSpec().flushMicrotasks();
2044 }
2045 Zone[api.symbol('fakeAsyncTest')] =
2046 { resetFakeAsyncZone, flushMicrotasks, discardPeriodicTasks, tick, flush, fakeAsync };
2047}, true);
2048
2049/**
2050 * @license
2051 * Copyright Google LLC All Rights Reserved.
2052 *
2053 * Use of this source code is governed by an MIT-style license that can be
2054 * found in the LICENSE file at https://angular.io/license
2055 */
2056/**
2057 * Promise for async/fakeAsync zoneSpec test
2058 * can support async operation which not supported by zone.js
2059 * such as
2060 * it ('test jsonp in AsyncZone', async() => {
2061 * new Promise(res => {
2062 * jsonp(url, (data) => {
2063 * // success callback
2064 * res(data);
2065 * });
2066 * }).then((jsonpResult) => {
2067 * // get jsonp result.
2068 *
2069 * // user will expect AsyncZoneSpec wait for
2070 * // then, but because jsonp is not zone aware
2071 * // AsyncZone will finish before then is called.
2072 * });
2073 * });
2074 */
2075Zone.__load_patch('promisefortest', (global, Zone, api) => {
2076 const symbolState = api.symbol('state');
2077 const UNRESOLVED = null;
2078 const symbolParentUnresolved = api.symbol('parentUnresolved');
2079 // patch Promise.prototype.then to keep an internal
2080 // number for tracking unresolved chained promise
2081 // we will decrease this number when the parent promise
2082 // being resolved/rejected and chained promise was
2083 // scheduled as a microTask.
2084 // so we can know such kind of chained promise still
2085 // not resolved in AsyncTestZone
2086 Promise[api.symbol('patchPromiseForTest')] = function patchPromiseForTest() {
2087 let oriThen = Promise[Zone.__symbol__('ZonePromiseThen')];
2088 if (oriThen) {
2089 return;
2090 }
2091 oriThen = Promise[Zone.__symbol__('ZonePromiseThen')] = Promise.prototype.then;
2092 Promise.prototype.then = function () {
2093 const chained = oriThen.apply(this, arguments);
2094 if (this[symbolState] === UNRESOLVED) {
2095 // parent promise is unresolved.
2096 const asyncTestZoneSpec = Zone.current.get('AsyncTestZoneSpec');
2097 if (asyncTestZoneSpec) {
2098 asyncTestZoneSpec.unresolvedChainedPromiseCount++;
2099 chained[symbolParentUnresolved] = true;
2100 }
2101 }
2102 return chained;
2103 };
2104 };
2105 Promise[api.symbol('unPatchPromiseForTest')] = function unpatchPromiseForTest() {
2106 // restore origin then
2107 const oriThen = Promise[Zone.__symbol__('ZonePromiseThen')];
2108 if (oriThen) {
2109 Promise.prototype.then = oriThen;
2110 Promise[Zone.__symbol__('ZonePromiseThen')] = undefined;
2111 }
2112 };
2113});
Note: See TracBrowser for help on using the repository browser.