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