jasmine-patch.umd.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. 'use strict';
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. /**
  14. * @license Angular v<unknown>
  15. * (c) 2010-2022 Google LLC. https://angular.io/
  16. * License: MIT
  17. */
  18. (function (factory) {
  19. typeof define === 'function' && define.amd ? define(factory) :
  20. factory();
  21. })((function () {
  22. 'use strict';
  23. /// <reference types="jasmine"/>
  24. Zone.__load_patch('jasmine', function (global, Zone, api) {
  25. var __extends = function (d, b) {
  26. for (var p in b)
  27. if (b.hasOwnProperty(p))
  28. d[p] = b[p];
  29. function __() {
  30. this.constructor = d;
  31. }
  32. d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
  33. };
  34. // Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs
  35. // in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503)
  36. if (!Zone)
  37. throw new Error('Missing: zone.js');
  38. if (typeof jest !== 'undefined') {
  39. // return if jasmine is a light implementation inside jest
  40. // in this case, we are running inside jest not jasmine
  41. return;
  42. }
  43. if (typeof jasmine == 'undefined' || jasmine['__zone_patch__']) {
  44. return;
  45. }
  46. jasmine['__zone_patch__'] = true;
  47. var SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
  48. var ProxyZoneSpec = Zone['ProxyZoneSpec'];
  49. if (!SyncTestZoneSpec)
  50. throw new Error('Missing: SyncTestZoneSpec');
  51. if (!ProxyZoneSpec)
  52. throw new Error('Missing: ProxyZoneSpec');
  53. var ambientZone = Zone.current;
  54. var symbol = Zone.__symbol__;
  55. // whether patch jasmine clock when in fakeAsync
  56. var disablePatchingJasmineClock = global[symbol('fakeAsyncDisablePatchingClock')] === true;
  57. // the original variable name fakeAsyncPatchLock is not accurate, so the name will be
  58. // fakeAsyncAutoFakeAsyncWhenClockPatched and if this enablePatchingJasmineClock is false, we also
  59. // automatically disable the auto jump into fakeAsync feature
  60. var enableAutoFakeAsyncWhenClockPatched = !disablePatchingJasmineClock &&
  61. ((global[symbol('fakeAsyncPatchLock')] === true) ||
  62. (global[symbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] === true));
  63. var ignoreUnhandledRejection = global[symbol('ignoreUnhandledRejection')] === true;
  64. if (!ignoreUnhandledRejection) {
  65. var globalErrors_1 = jasmine.GlobalErrors;
  66. if (globalErrors_1 && !jasmine[symbol('GlobalErrors')]) {
  67. jasmine[symbol('GlobalErrors')] = globalErrors_1;
  68. jasmine.GlobalErrors = function () {
  69. var instance = new globalErrors_1();
  70. var originalInstall = instance.install;
  71. if (originalInstall && !instance[symbol('install')]) {
  72. instance[symbol('install')] = originalInstall;
  73. instance.install = function () {
  74. var isNode = typeof process !== 'undefined' && !!process.on;
  75. // Note: Jasmine checks internally if `process` and `process.on` is defined. Otherwise,
  76. // it installs the browser rejection handler through the `global.addEventListener`.
  77. // This code may be run in the browser environment where `process` is not defined, and
  78. // this will lead to a runtime exception since Webpack 5 removed automatic Node.js
  79. // polyfills. Note, that events are named differently, it's `unhandledRejection` in
  80. // Node.js and `unhandledrejection` in the browser.
  81. var originalHandlers = isNode ? process.listeners('unhandledRejection') :
  82. global.eventListeners('unhandledrejection');
  83. var result = originalInstall.apply(this, arguments);
  84. isNode ? process.removeAllListeners('unhandledRejection') :
  85. global.removeAllListeners('unhandledrejection');
  86. if (originalHandlers) {
  87. originalHandlers.forEach(function (handler) {
  88. if (isNode) {
  89. process.on('unhandledRejection', handler);
  90. }
  91. else {
  92. global.addEventListener('unhandledrejection', handler);
  93. }
  94. });
  95. }
  96. return result;
  97. };
  98. }
  99. return instance;
  100. };
  101. }
  102. }
  103. // Monkey patch all of the jasmine DSL so that each function runs in appropriate zone.
  104. var jasmineEnv = jasmine.getEnv();
  105. ['describe', 'xdescribe', 'fdescribe'].forEach(function (methodName) {
  106. var originalJasmineFn = jasmineEnv[methodName];
  107. jasmineEnv[methodName] = function (description, specDefinitions) {
  108. return originalJasmineFn.call(this, description, wrapDescribeInZone(description, specDefinitions));
  109. };
  110. });
  111. ['it', 'xit', 'fit'].forEach(function (methodName) {
  112. var originalJasmineFn = jasmineEnv[methodName];
  113. jasmineEnv[symbol(methodName)] = originalJasmineFn;
  114. jasmineEnv[methodName] = function (description, specDefinitions, timeout) {
  115. arguments[1] = wrapTestInZone(specDefinitions);
  116. return originalJasmineFn.apply(this, arguments);
  117. };
  118. });
  119. ['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(function (methodName) {
  120. var originalJasmineFn = jasmineEnv[methodName];
  121. jasmineEnv[symbol(methodName)] = originalJasmineFn;
  122. jasmineEnv[methodName] = function (specDefinitions, timeout) {
  123. arguments[0] = wrapTestInZone(specDefinitions);
  124. return originalJasmineFn.apply(this, arguments);
  125. };
  126. });
  127. if (!disablePatchingJasmineClock) {
  128. // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so
  129. // they can work properly in FakeAsyncTest
  130. var originalClockFn_1 = (jasmine[symbol('clock')] = jasmine['clock']);
  131. jasmine['clock'] = function () {
  132. var clock = originalClockFn_1.apply(this, arguments);
  133. if (!clock[symbol('patched')]) {
  134. clock[symbol('patched')] = symbol('patched');
  135. var originalTick_1 = (clock[symbol('tick')] = clock.tick);
  136. clock.tick = function () {
  137. var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
  138. if (fakeAsyncZoneSpec) {
  139. return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments);
  140. }
  141. return originalTick_1.apply(this, arguments);
  142. };
  143. var originalMockDate_1 = (clock[symbol('mockDate')] = clock.mockDate);
  144. clock.mockDate = function () {
  145. var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
  146. if (fakeAsyncZoneSpec) {
  147. var dateTime = arguments.length > 0 ? arguments[0] : new Date();
  148. return fakeAsyncZoneSpec.setFakeBaseSystemTime.apply(fakeAsyncZoneSpec, dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
  149. arguments);
  150. }
  151. return originalMockDate_1.apply(this, arguments);
  152. };
  153. // for auto go into fakeAsync feature, we need the flag to enable it
  154. if (enableAutoFakeAsyncWhenClockPatched) {
  155. ['install', 'uninstall'].forEach(function (methodName) {
  156. var originalClockFn = (clock[symbol(methodName)] = clock[methodName]);
  157. clock[methodName] = function () {
  158. var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec'];
  159. if (FakeAsyncTestZoneSpec) {
  160. jasmine[symbol('clockInstalled')] = 'install' === methodName;
  161. return;
  162. }
  163. return originalClockFn.apply(this, arguments);
  164. };
  165. });
  166. }
  167. }
  168. return clock;
  169. };
  170. }
  171. // monkey patch createSpyObj to make properties enumerable to true
  172. if (!jasmine[Zone.__symbol__('createSpyObj')]) {
  173. var originalCreateSpyObj_1 = jasmine.createSpyObj;
  174. jasmine[Zone.__symbol__('createSpyObj')] = originalCreateSpyObj_1;
  175. jasmine.createSpyObj = function () {
  176. var args = Array.prototype.slice.call(arguments);
  177. var propertyNames = args.length >= 3 ? args[2] : null;
  178. var spyObj;
  179. if (propertyNames) {
  180. var defineProperty_1 = Object.defineProperty;
  181. Object.defineProperty = function (obj, p, attributes) {
  182. return defineProperty_1.call(this, obj, p, __assign(__assign({}, attributes), { configurable: true, enumerable: true }));
  183. };
  184. try {
  185. spyObj = originalCreateSpyObj_1.apply(this, args);
  186. }
  187. finally {
  188. Object.defineProperty = defineProperty_1;
  189. }
  190. }
  191. else {
  192. spyObj = originalCreateSpyObj_1.apply(this, args);
  193. }
  194. return spyObj;
  195. };
  196. }
  197. /**
  198. * Gets a function wrapping the body of a Jasmine `describe` block to execute in a
  199. * synchronous-only zone.
  200. */
  201. function wrapDescribeInZone(description, describeBody) {
  202. return function () {
  203. // Create a synchronous-only zone in which to run `describe` blocks in order to raise an
  204. // error if any asynchronous operations are attempted inside of a `describe`.
  205. var syncZone = ambientZone.fork(new SyncTestZoneSpec("jasmine.describe#".concat(description)));
  206. return syncZone.run(describeBody, this, arguments);
  207. };
  208. }
  209. function runInTestZone(testBody, applyThis, queueRunner, done) {
  210. var isClockInstalled = !!jasmine[symbol('clockInstalled')];
  211. queueRunner.testProxyZoneSpec;
  212. var testProxyZone = queueRunner.testProxyZone;
  213. if (isClockInstalled && enableAutoFakeAsyncWhenClockPatched) {
  214. // auto run a fakeAsync
  215. var fakeAsyncModule = Zone[Zone.__symbol__('fakeAsyncTest')];
  216. if (fakeAsyncModule && typeof fakeAsyncModule.fakeAsync === 'function') {
  217. testBody = fakeAsyncModule.fakeAsync(testBody);
  218. }
  219. }
  220. if (done) {
  221. return testProxyZone.run(testBody, applyThis, [done]);
  222. }
  223. else {
  224. return testProxyZone.run(testBody, applyThis);
  225. }
  226. }
  227. /**
  228. * Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to
  229. * execute in a ProxyZone zone.
  230. * This will run in `testProxyZone`. The `testProxyZone` will be reset by the `ZoneQueueRunner`
  231. */
  232. function wrapTestInZone(testBody) {
  233. // The `done` callback is only passed through if the function expects at least one argument.
  234. // Note we have to make a function with correct number of arguments, otherwise jasmine will
  235. // think that all functions are sync or async.
  236. return (testBody && (testBody.length ? function (done) {
  237. return runInTestZone(testBody, this, this.queueRunner, done);
  238. } : function () {
  239. return runInTestZone(testBody, this, this.queueRunner);
  240. }));
  241. }
  242. var QueueRunner = jasmine.QueueRunner;
  243. jasmine.QueueRunner = (function (_super) {
  244. __extends(ZoneQueueRunner, _super);
  245. function ZoneQueueRunner(attrs) {
  246. var _this = this;
  247. if (attrs.onComplete) {
  248. attrs.onComplete = (function (fn) { return function () {
  249. // All functions are done, clear the test zone.
  250. _this.testProxyZone = null;
  251. _this.testProxyZoneSpec = null;
  252. ambientZone.scheduleMicroTask('jasmine.onComplete', fn);
  253. }; })(attrs.onComplete);
  254. }
  255. var nativeSetTimeout = global[Zone.__symbol__('setTimeout')];
  256. var nativeClearTimeout = global[Zone.__symbol__('clearTimeout')];
  257. if (nativeSetTimeout) {
  258. // should run setTimeout inside jasmine outside of zone
  259. attrs.timeout = {
  260. setTimeout: nativeSetTimeout ? nativeSetTimeout : global.setTimeout,
  261. clearTimeout: nativeClearTimeout ? nativeClearTimeout : global.clearTimeout
  262. };
  263. }
  264. // create a userContext to hold the queueRunner itself
  265. // so we can access the testProxy in it/xit/beforeEach ...
  266. if (jasmine.UserContext) {
  267. if (!attrs.userContext) {
  268. attrs.userContext = new jasmine.UserContext();
  269. }
  270. attrs.userContext.queueRunner = this;
  271. }
  272. else {
  273. if (!attrs.userContext) {
  274. attrs.userContext = {};
  275. }
  276. attrs.userContext.queueRunner = this;
  277. }
  278. // patch attrs.onException
  279. var onException = attrs.onException;
  280. attrs.onException = function (error) {
  281. if (error &&
  282. error.message ===
  283. 'Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.') {
  284. // jasmine timeout, we can make the error message more
  285. // reasonable to tell what tasks are pending
  286. var proxyZoneSpec = this && this.testProxyZoneSpec;
  287. if (proxyZoneSpec) {
  288. var pendingTasksInfo = proxyZoneSpec.getAndClearPendingTasksInfo();
  289. try {
  290. // try catch here in case error.message is not writable
  291. error.message += pendingTasksInfo;
  292. }
  293. catch (err) {
  294. }
  295. }
  296. }
  297. if (onException) {
  298. onException.call(this, error);
  299. }
  300. };
  301. _super.call(this, attrs);
  302. }
  303. ZoneQueueRunner.prototype.execute = function () {
  304. var _this = this;
  305. var zone = Zone.current;
  306. var isChildOfAmbientZone = false;
  307. while (zone) {
  308. if (zone === ambientZone) {
  309. isChildOfAmbientZone = true;
  310. break;
  311. }
  312. zone = zone.parent;
  313. }
  314. if (!isChildOfAmbientZone)
  315. throw new Error('Unexpected Zone: ' + Zone.current.name);
  316. // This is the zone which will be used for running individual tests.
  317. // It will be a proxy zone, so that the tests function can retroactively install
  318. // different zones.
  319. // Example:
  320. // - In beforeEach() do childZone = Zone.current.fork(...);
  321. // - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the
  322. // zone outside of fakeAsync it will be able to escape the fakeAsync rules.
  323. // - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add
  324. // fakeAsync behavior to the childZone.
  325. this.testProxyZoneSpec = new ProxyZoneSpec();
  326. this.testProxyZone = ambientZone.fork(this.testProxyZoneSpec);
  327. if (!Zone.currentTask) {
  328. // if we are not running in a task then if someone would register a
  329. // element.addEventListener and then calling element.click() the
  330. // addEventListener callback would think that it is the top most task and would
  331. // drain the microtask queue on element.click() which would be incorrect.
  332. // For this reason we always force a task when running jasmine tests.
  333. Zone.current.scheduleMicroTask('jasmine.execute().forceTask', function () { return QueueRunner.prototype.execute.call(_this); });
  334. }
  335. else {
  336. _super.prototype.execute.call(this);
  337. }
  338. };
  339. return ZoneQueueRunner;
  340. })(QueueRunner);
  341. });
  342. }));