async-test.js 13 KB

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