mocha-patch.umd.js 6.3 KB

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