async-test.umd.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. (function (factory) {
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. factory();
  10. })((function () {
  11. 'use strict';
  12. var global$1 = globalThis;
  13. // __Zone_symbol_prefix global can be used to override the default zone
  14. // symbol prefix with a custom one if needed.
  15. function __symbol__(name) {
  16. var symbolPrefix = global$1['__Zone_symbol_prefix'] || '__zone_symbol__';
  17. return symbolPrefix + name;
  18. }
  19. var __global = (typeof window !== 'undefined' && window) || (typeof self !== 'undefined' && self) || global;
  20. var AsyncTestZoneSpec = /** @class */ (function () {
  21. function AsyncTestZoneSpec(finishCallback, failCallback, namePrefix) {
  22. this._pendingMicroTasks = false;
  23. this._pendingMacroTasks = false;
  24. this._alreadyErrored = false;
  25. this._isSync = false;
  26. this._existingFinishTimer = null;
  27. this.entryFunction = null;
  28. this.runZone = Zone.current;
  29. this.unresolvedChainedPromiseCount = 0;
  30. this.supportWaitUnresolvedChainedPromise = false;
  31. this.finishCallback = finishCallback;
  32. this.failCallback = failCallback;
  33. this.name = 'asyncTestZone for ' + namePrefix;
  34. this.properties = { 'AsyncTestZoneSpec': this };
  35. this.supportWaitUnresolvedChainedPromise =
  36. __global[__symbol__('supportWaitUnResolvedChainedPromise')] === true;
  37. }
  38. Object.defineProperty(AsyncTestZoneSpec, "symbolParentUnresolved", {
  39. // Needs to be a getter and not a plain property in order run this just-in-time. Otherwise
  40. // `__symbol__` would be evaluated during top-level execution prior to the Zone prefix being
  41. // changed for tests.
  42. get: function () {
  43. return __symbol__('parentUnresolved');
  44. },
  45. enumerable: false,
  46. configurable: true
  47. });
  48. AsyncTestZoneSpec.prototype.isUnresolvedChainedPromisePending = function () {
  49. return this.unresolvedChainedPromiseCount > 0;
  50. };
  51. AsyncTestZoneSpec.prototype._finishCallbackIfDone = function () {
  52. var _this = this;
  53. // NOTE: Technically the `onHasTask` could fire together with the initial synchronous
  54. // completion in `onInvoke`. `onHasTask` might call this method when it captured e.g.
  55. // microtasks in the proxy zone that now complete as part of this async zone run.
  56. // Consider the following scenario:
  57. // 1. A test `beforeEach` schedules a microtask in the ProxyZone.
  58. // 2. An actual empty `it` spec executes in the AsyncTestZone` (using e.g. `waitForAsync`).
  59. // 3. The `onInvoke` invokes `_finishCallbackIfDone` because the spec runs synchronously.
  60. // 4. We wait the scheduled timeout (see below) to account for unhandled promises.
  61. // 5. The microtask from (1) finishes and `onHasTask` is invoked.
  62. // --> We register a second `_finishCallbackIfDone` even though we have scheduled a timeout.
  63. // If the finish timeout from below is already scheduled, terminate the existing scheduled
  64. // finish invocation, avoiding calling `jasmine` `done` multiple times. *Note* that we would
  65. // want to schedule a new finish callback in case the task state changes again.
  66. if (this._existingFinishTimer !== null) {
  67. clearTimeout(this._existingFinishTimer);
  68. this._existingFinishTimer = null;
  69. }
  70. if (!(this._pendingMicroTasks ||
  71. this._pendingMacroTasks ||
  72. (this.supportWaitUnresolvedChainedPromise && this.isUnresolvedChainedPromisePending()))) {
  73. // We wait until the next tick because we would like to catch unhandled promises which could
  74. // cause test logic to be executed. In such cases we cannot finish with tasks pending then.
  75. this.runZone.run(function () {
  76. _this._existingFinishTimer = setTimeout(function () {
  77. if (!_this._alreadyErrored && !(_this._pendingMicroTasks || _this._pendingMacroTasks)) {
  78. _this.finishCallback();
  79. }
  80. }, 0);
  81. });
  82. }
  83. };
  84. AsyncTestZoneSpec.prototype.patchPromiseForTest = function () {
  85. if (!this.supportWaitUnresolvedChainedPromise) {
  86. return;
  87. }
  88. var patchPromiseForTest = Promise[Zone.__symbol__('patchPromiseForTest')];
  89. if (patchPromiseForTest) {
  90. patchPromiseForTest();
  91. }
  92. };
  93. AsyncTestZoneSpec.prototype.unPatchPromiseForTest = function () {
  94. if (!this.supportWaitUnresolvedChainedPromise) {
  95. return;
  96. }
  97. var unPatchPromiseForTest = Promise[Zone.__symbol__('unPatchPromiseForTest')];
  98. if (unPatchPromiseForTest) {
  99. unPatchPromiseForTest();
  100. }
  101. };
  102. AsyncTestZoneSpec.prototype.onScheduleTask = function (delegate, current, target, task) {
  103. if (task.type !== 'eventTask') {
  104. this._isSync = false;
  105. }
  106. if (task.type === 'microTask' && task.data && task.data instanceof Promise) {
  107. // check whether the promise is a chained promise
  108. if (task.data[AsyncTestZoneSpec.symbolParentUnresolved] === true) {
  109. // chained promise is being scheduled
  110. this.unresolvedChainedPromiseCount--;
  111. }
  112. }
  113. return delegate.scheduleTask(target, task);
  114. };
  115. AsyncTestZoneSpec.prototype.onInvokeTask = function (delegate, current, target, task, applyThis, applyArgs) {
  116. if (task.type !== 'eventTask') {
  117. this._isSync = false;
  118. }
  119. return delegate.invokeTask(target, task, applyThis, applyArgs);
  120. };
  121. AsyncTestZoneSpec.prototype.onCancelTask = function (delegate, current, target, task) {
  122. if (task.type !== 'eventTask') {
  123. this._isSync = false;
  124. }
  125. return delegate.cancelTask(target, task);
  126. };
  127. // Note - we need to use onInvoke at the moment to call finish when a test is
  128. // fully synchronous. TODO(juliemr): remove this when the logic for
  129. // onHasTask changes and it calls whenever the task queues are dirty.
  130. // updated by(JiaLiPassion), only call finish callback when no task
  131. // was scheduled/invoked/canceled.
  132. AsyncTestZoneSpec.prototype.onInvoke = function (parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
  133. if (!this.entryFunction) {
  134. this.entryFunction = delegate;
  135. }
  136. try {
  137. this._isSync = true;
  138. return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
  139. }
  140. finally {
  141. // We need to check the delegate is the same as entryFunction or not.
  142. // Consider the following case.
  143. //
  144. // asyncTestZone.run(() => { // Here the delegate will be the entryFunction
  145. // Zone.current.run(() => { // Here the delegate will not be the entryFunction
  146. // });
  147. // });
  148. //
  149. // We only want to check whether there are async tasks scheduled
  150. // for the entry function.
  151. if (this._isSync && this.entryFunction === delegate) {
  152. this._finishCallbackIfDone();
  153. }
  154. }
  155. };
  156. AsyncTestZoneSpec.prototype.onHandleError = function (parentZoneDelegate, currentZone, targetZone, error) {
  157. // Let the parent try to handle the error.
  158. var result = parentZoneDelegate.handleError(targetZone, error);
  159. if (result) {
  160. this.failCallback(error);
  161. this._alreadyErrored = true;
  162. }
  163. return false;
  164. };
  165. AsyncTestZoneSpec.prototype.onHasTask = function (delegate, current, target, hasTaskState) {
  166. delegate.hasTask(target, hasTaskState);
  167. // We should only trigger finishCallback when the target zone is the AsyncTestZone
  168. // Consider the following cases.
  169. //
  170. // const childZone = asyncTestZone.fork({
  171. // name: 'child',
  172. // onHasTask: ...
  173. // });
  174. //
  175. // So we have nested zones declared the onHasTask hook, in this case,
  176. // the onHasTask will be triggered twice, and cause the finishCallbackIfDone()
  177. // is also be invoked twice. So we need to only trigger the finishCallbackIfDone()
  178. // when the current zone is the same as the target zone.
  179. if (current !== target) {
  180. return;
  181. }
  182. if (hasTaskState.change == 'microTask') {
  183. this._pendingMicroTasks = hasTaskState.microTask;
  184. this._finishCallbackIfDone();
  185. }
  186. else if (hasTaskState.change == 'macroTask') {
  187. this._pendingMacroTasks = hasTaskState.macroTask;
  188. this._finishCallbackIfDone();
  189. }
  190. };
  191. return AsyncTestZoneSpec;
  192. }());
  193. function patchAsyncTest(Zone) {
  194. // Export the class so that new instances can be created with proper
  195. // constructor params.
  196. Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
  197. Zone.__load_patch('asynctest', function (global, Zone, api) {
  198. /**
  199. * Wraps a test function in an asynchronous test zone. The test will automatically
  200. * complete when all asynchronous calls within this zone are done.
  201. */
  202. Zone[api.symbol('asyncTest')] = function asyncTest(fn) {
  203. // If we're running using the Jasmine test framework, adapt to call the 'done'
  204. // function when asynchronous activity is finished.
  205. if (global.jasmine) {
  206. // Not using an arrow function to preserve context passed from call site
  207. return function (done) {
  208. if (!done) {
  209. // if we run beforeEach in @angular/core/testing/testing_internal then we get no done
  210. // fake it here and assume sync.
  211. done = function () { };
  212. done.fail = function (e) {
  213. throw e;
  214. };
  215. }
  216. runInTestZone(fn, this, done, function (err) {
  217. if (typeof err === 'string') {
  218. return done.fail(new Error(err));
  219. }
  220. else {
  221. done.fail(err);
  222. }
  223. });
  224. };
  225. }
  226. // Otherwise, return a promise which will resolve when asynchronous activity
  227. // is finished. This will be correctly consumed by the Mocha framework with
  228. // it('...', async(myFn)); or can be used in a custom framework.
  229. // Not using an arrow function to preserve context passed from call site
  230. return function () {
  231. var _this = this;
  232. return new Promise(function (finishCallback, failCallback) {
  233. runInTestZone(fn, _this, finishCallback, failCallback);
  234. });
  235. };
  236. };
  237. function runInTestZone(fn, context, finishCallback, failCallback) {
  238. var currentZone = Zone.current;
  239. var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
  240. if (AsyncTestZoneSpec === undefined) {
  241. throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
  242. 'Please make sure that your environment includes zone.js/plugins/async-test');
  243. }
  244. var ProxyZoneSpec = Zone['ProxyZoneSpec'];
  245. if (!ProxyZoneSpec) {
  246. throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
  247. 'Please make sure that your environment includes zone.js/plugins/proxy');
  248. }
  249. var proxyZoneSpec = ProxyZoneSpec.get();
  250. ProxyZoneSpec.assertPresent();
  251. // We need to create the AsyncTestZoneSpec outside the ProxyZone.
  252. // If we do it in ProxyZone then we will get to infinite recursion.
  253. var proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
  254. var previousDelegate = proxyZoneSpec.getDelegate();
  255. proxyZone.parent.run(function () {
  256. var testZoneSpec = new AsyncTestZoneSpec(function () {
  257. // Need to restore the original zone.
  258. if (proxyZoneSpec.getDelegate() == testZoneSpec) {
  259. // Only reset the zone spec if it's
  260. // still this one. Otherwise, assume
  261. // it's OK.
  262. proxyZoneSpec.setDelegate(previousDelegate);
  263. }
  264. testZoneSpec.unPatchPromiseForTest();
  265. currentZone.run(function () {
  266. finishCallback();
  267. });
  268. }, function (error) {
  269. // Need to restore the original zone.
  270. if (proxyZoneSpec.getDelegate() == testZoneSpec) {
  271. // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
  272. proxyZoneSpec.setDelegate(previousDelegate);
  273. }
  274. testZoneSpec.unPatchPromiseForTest();
  275. currentZone.run(function () {
  276. failCallback(error);
  277. });
  278. }, 'test');
  279. proxyZoneSpec.setDelegate(testZoneSpec);
  280. testZoneSpec.patchPromiseForTest();
  281. });
  282. return Zone.current.runGuarded(fn, context);
  283. }
  284. });
  285. }
  286. patchAsyncTest(Zone);
  287. }));