jasmine-patch.umd.js 19 KB

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