async-test.js 12 KB

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