mocha-patch.umd.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. function patchMocha(Zone) {
  13. Zone.__load_patch('mocha', function (global, Zone) {
  14. var Mocha = global.Mocha;
  15. if (typeof Mocha === 'undefined') {
  16. // return if Mocha is not available, because now zone-testing
  17. // will load mocha patch with jasmine/jest patch
  18. return;
  19. }
  20. if (typeof Zone === 'undefined') {
  21. throw new Error('Missing Zone.js');
  22. }
  23. var ProxyZoneSpec = Zone['ProxyZoneSpec'];
  24. var SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
  25. if (!ProxyZoneSpec) {
  26. throw new Error('Missing ProxyZoneSpec');
  27. }
  28. if (Mocha['__zone_patch__']) {
  29. throw new Error('"Mocha" has already been patched with "Zone".');
  30. }
  31. Mocha['__zone_patch__'] = true;
  32. var rootZone = Zone.current;
  33. var syncZone = rootZone.fork(new SyncTestZoneSpec('Mocha.describe'));
  34. var testZone = null;
  35. var suiteZone = rootZone.fork(new ProxyZoneSpec());
  36. var mochaOriginal = {
  37. after: global.after,
  38. afterEach: global.afterEach,
  39. before: global.before,
  40. beforeEach: global.beforeEach,
  41. describe: global.describe,
  42. it: global.it,
  43. };
  44. function modifyArguments(args, syncTest, asyncTest) {
  45. var _loop_1 = function (i) {
  46. var arg = args[i];
  47. if (typeof arg === 'function') {
  48. // The `done` callback is only passed through if the function expects at
  49. // least one argument.
  50. // Note we have to make a function with correct number of arguments,
  51. // otherwise mocha will
  52. // think that all functions are sync or async.
  53. args[i] = arg.length === 0 ? syncTest(arg) : asyncTest(arg);
  54. // Mocha uses toString to view the test body in the result list, make sure we return the
  55. // correct function body
  56. args[i].toString = function () {
  57. return arg.toString();
  58. };
  59. }
  60. };
  61. for (var i = 0; i < args.length; i++) {
  62. _loop_1(i);
  63. }
  64. return args;
  65. }
  66. function wrapDescribeInZone(args) {
  67. var syncTest = function (fn) {
  68. return function () {
  69. return syncZone.run(fn, this, arguments);
  70. };
  71. };
  72. return modifyArguments(args, syncTest);
  73. }
  74. function wrapTestInZone(args) {
  75. var asyncTest = function (fn) {
  76. return function (done) {
  77. return testZone.run(fn, this, [done]);
  78. };
  79. };
  80. var syncTest = function (fn) {
  81. return function () {
  82. return testZone.run(fn, this);
  83. };
  84. };
  85. return modifyArguments(args, syncTest, asyncTest);
  86. }
  87. function wrapSuiteInZone(args) {
  88. var asyncTest = function (fn) {
  89. return function (done) {
  90. return suiteZone.run(fn, this, [done]);
  91. };
  92. };
  93. var syncTest = function (fn) {
  94. return function () {
  95. return suiteZone.run(fn, this);
  96. };
  97. };
  98. return modifyArguments(args, syncTest, asyncTest);
  99. }
  100. global.describe = global.suite = function () {
  101. return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments));
  102. };
  103. global.xdescribe =
  104. global.suite.skip =
  105. global.describe.skip =
  106. function () {
  107. return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments));
  108. };
  109. global.describe.only = global.suite.only = function () {
  110. return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments));
  111. };
  112. global.it =
  113. global.specify =
  114. global.test =
  115. function () {
  116. return mochaOriginal.it.apply(this, wrapTestInZone(arguments));
  117. };
  118. global.xit =
  119. global.xspecify =
  120. global.it.skip =
  121. function () {
  122. return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments));
  123. };
  124. global.it.only = global.test.only = function () {
  125. return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments));
  126. };
  127. global.after = global.suiteTeardown = function () {
  128. return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments));
  129. };
  130. global.afterEach = global.teardown = function () {
  131. return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments));
  132. };
  133. global.before = global.suiteSetup = function () {
  134. return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments));
  135. };
  136. global.beforeEach = global.setup = function () {
  137. return mochaOriginal.beforeEach.apply(this, wrapTestInZone(arguments));
  138. };
  139. (function (originalRunTest, originalRun) {
  140. Mocha.Runner.prototype.runTest = function (fn) {
  141. var _this = this;
  142. Zone.current.scheduleMicroTask('mocha.forceTask', function () {
  143. originalRunTest.call(_this, fn);
  144. });
  145. };
  146. Mocha.Runner.prototype.run = function (fn) {
  147. this.on('test', function (e) {
  148. testZone = rootZone.fork(new ProxyZoneSpec());
  149. });
  150. this.on('fail', function (test, err) {
  151. var proxyZoneSpec = testZone && testZone.get('ProxyZoneSpec');
  152. if (proxyZoneSpec && err) {
  153. try {
  154. // try catch here in case err.message is not writable
  155. err.message += proxyZoneSpec.getAndClearPendingTasksInfo();
  156. }
  157. catch (error) { }
  158. }
  159. });
  160. return originalRun.call(this, fn);
  161. };
  162. })(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run);
  163. });
  164. }
  165. patchMocha(Zone);
  166. }));