|
| 1 | +/** |
| 2 | +* @license |
| 3 | +* Copyright Google Inc. All Rights Reserved. |
| 4 | +* |
| 5 | +* Use of this source code is governed by an MIT-style license that can be |
| 6 | +* found in the LICENSE file at https://angular.io/license |
| 7 | +*/ |
| 8 | +(function (global, factory) { |
| 9 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
| 10 | + typeof define === 'function' && define.amd ? define(factory) : |
| 11 | + (factory()); |
| 12 | +}(this, (function () { 'use strict'; |
| 13 | + |
| 14 | +(function (context) { |
| 15 | + var Mocha = context.Mocha; |
| 16 | + if (typeof Mocha === 'undefined') { |
| 17 | + throw new Error('Missing Mocha.js'); |
| 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: Mocha.after, |
| 37 | + afterEach: Mocha.afterEach, |
| 38 | + before: Mocha.before, |
| 39 | + beforeEach: Mocha.beforeEach, |
| 40 | + describe: Mocha.describe, |
| 41 | + it: Mocha.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 correct function body |
| 54 | + args[i].toString = function () { |
| 55 | + return arg.toString(); |
| 56 | + }; |
| 57 | + } |
| 58 | + }; |
| 59 | + for (var i = 0; i < args.length; i++) { |
| 60 | + _loop_1(i); |
| 61 | + } |
| 62 | + return args; |
| 63 | + } |
| 64 | + function wrapDescribeInZone(args) { |
| 65 | + var syncTest = function (fn) { |
| 66 | + return function () { |
| 67 | + return syncZone.run(fn, this, arguments); |
| 68 | + }; |
| 69 | + }; |
| 70 | + return modifyArguments(args, syncTest); |
| 71 | + } |
| 72 | + function wrapTestInZone(args) { |
| 73 | + var asyncTest = function (fn) { |
| 74 | + return function (done) { |
| 75 | + return testZone.run(fn, this, [done]); |
| 76 | + }; |
| 77 | + }; |
| 78 | + var syncTest = function (fn) { |
| 79 | + return function () { |
| 80 | + return testZone.run(fn, this); |
| 81 | + }; |
| 82 | + }; |
| 83 | + return modifyArguments(args, syncTest, asyncTest); |
| 84 | + } |
| 85 | + function wrapSuiteInZone(args) { |
| 86 | + var asyncTest = function (fn) { |
| 87 | + return function (done) { |
| 88 | + return suiteZone.run(fn, this, [done]); |
| 89 | + }; |
| 90 | + }; |
| 91 | + var syncTest = function (fn) { |
| 92 | + return function () { |
| 93 | + return suiteZone.run(fn, this); |
| 94 | + }; |
| 95 | + }; |
| 96 | + return modifyArguments(args, syncTest, asyncTest); |
| 97 | + } |
| 98 | + |
| 99 | + context.describe = context.suite = Mocha.describe = function () { |
| 100 | + return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments)); |
| 101 | + }; |
| 102 | + context.xdescribe = context.suite.skip = Mocha.describe.skip = function () { |
| 103 | + return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments)); |
| 104 | + }; |
| 105 | + context.describe.only = context.suite.only = Mocha.describe.only = function () { |
| 106 | + return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments)); |
| 107 | + }; |
| 108 | + context.it = context.specify = context.test = Mocha.it = function () { |
| 109 | + return mochaOriginal.it.apply(this, wrapTestInZone(arguments)); |
| 110 | + }; |
| 111 | + context.xit = context.xspecify = Mocha.it.skip = function () { |
| 112 | + return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments)); |
| 113 | + }; |
| 114 | + context.it.only = context.test.only = Mocha.it.only = function () { |
| 115 | + return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments)); |
| 116 | + }; |
| 117 | + context.after = context.suiteTeardown = Mocha.after = function () { |
| 118 | + return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments)); |
| 119 | + }; |
| 120 | + context.afterEach = context.teardown = Mocha.afterEach = function () { |
| 121 | + return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments)); |
| 122 | + }; |
| 123 | + context.before = context.suiteSetup = Mocha.before = function () { |
| 124 | + return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments)); |
| 125 | + }; |
| 126 | + context.beforeEach = context.setup = Mocha.beforeEach = 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 | + if (Zone.current !== rootZone) { |
| 139 | + throw new Error('Unexpected zone: ' + Zone.current.name); |
| 140 | + } |
| 141 | + testZone = rootZone.fork(new ProxyZoneSpec()); |
| 142 | + }); |
| 143 | + return originalRun.call(this, fn); |
| 144 | + }; |
| 145 | + })(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run); |
| 146 | +})(window); |
| 147 | + |
| 148 | +}))); |
0 commit comments