mocha-patch.js 5.5 KB

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