2019-05-31 11:56:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
Zone.__load_patch('mocha', (global: any, Zone: ZoneType) => {
|
|
|
|
const Mocha = global.Mocha;
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
if (typeof Mocha === 'undefined') {
|
2020-01-31 08:54:44 -05:00
|
|
|
// return if Mocha is not available, because now zone-testing
|
|
|
|
// will load mocha patch with jasmine/jest patch
|
|
|
|
return;
|
2019-05-31 11:56:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof Zone === 'undefined') {
|
|
|
|
throw new Error('Missing Zone.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec'];
|
|
|
|
const SyncTestZoneSpec = (Zone as any)['SyncTestZoneSpec'];
|
|
|
|
|
|
|
|
if (!ProxyZoneSpec) {
|
|
|
|
throw new Error('Missing ProxyZoneSpec');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Mocha['__zone_patch__']) {
|
|
|
|
throw new Error('"Mocha" has already been patched with "Zone".');
|
|
|
|
}
|
|
|
|
|
|
|
|
Mocha['__zone_patch__'] = true;
|
|
|
|
|
|
|
|
const rootZone = Zone.current;
|
|
|
|
const syncZone = rootZone.fork(new SyncTestZoneSpec('Mocha.describe'));
|
|
|
|
let testZone: Zone|null = null;
|
|
|
|
const suiteZone = rootZone.fork(new ProxyZoneSpec());
|
|
|
|
|
|
|
|
const mochaOriginal = {
|
|
|
|
after: Mocha.after,
|
|
|
|
afterEach: Mocha.afterEach,
|
|
|
|
before: Mocha.before,
|
|
|
|
beforeEach: Mocha.beforeEach,
|
|
|
|
describe: Mocha.describe,
|
|
|
|
it: Mocha.it
|
|
|
|
};
|
|
|
|
|
|
|
|
function modifyArguments(args: IArguments, syncTest: Function, asyncTest?: Function): any[] {
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
let arg = args[i];
|
|
|
|
if (typeof arg === 'function') {
|
|
|
|
// The `done` callback is only passed through if the function expects at
|
|
|
|
// least one argument.
|
|
|
|
// Note we have to make a function with correct number of arguments,
|
|
|
|
// otherwise mocha will
|
|
|
|
// think that all functions are sync or async.
|
|
|
|
args[i] = (arg.length === 0) ? syncTest(arg) : asyncTest !(arg);
|
|
|
|
// Mocha uses toString to view the test body in the result list, make sure we return the
|
|
|
|
// correct function body
|
|
|
|
args[i].toString = function() { return arg.toString(); };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return args as any;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapDescribeInZone(args: IArguments): any[] {
|
|
|
|
const syncTest: any = function(fn: Function) {
|
2019-06-26 04:33:02 -04:00
|
|
|
return function(this: unknown) { return syncZone.run(fn, this, arguments as any as any[]); };
|
2019-05-31 11:56:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return modifyArguments(args, syncTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapTestInZone(args: IArguments): any[] {
|
|
|
|
const asyncTest = function(fn: Function) {
|
2019-06-26 04:33:02 -04:00
|
|
|
return function(this: unknown, done: Function) { return testZone !.run(fn, this, [done]); };
|
2019-05-31 11:56:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const syncTest: any = function(fn: Function) {
|
2019-06-26 04:33:02 -04:00
|
|
|
return function(this: unknown) { return testZone !.run(fn, this); };
|
2019-05-31 11:56:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return modifyArguments(args, syncTest, asyncTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapSuiteInZone(args: IArguments): any[] {
|
|
|
|
const asyncTest = function(fn: Function) {
|
2019-06-26 04:33:02 -04:00
|
|
|
return function(this: unknown, done: Function) { return suiteZone.run(fn, this, [done]); };
|
2019-05-31 11:56:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const syncTest: any = function(fn: Function) {
|
2019-06-26 04:33:02 -04:00
|
|
|
return function(this: unknown) { return suiteZone.run(fn, this); };
|
2019-05-31 11:56:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return modifyArguments(args, syncTest, asyncTest);
|
|
|
|
}
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.describe = global.suite = Mocha.describe = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.xdescribe = global.suite.skip = Mocha.describe.skip = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.describe.only = global.suite.only = Mocha.describe.only = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.it = global.specify = global.test =
|
2019-05-31 11:56:07 -04:00
|
|
|
Mocha.it = function() { return mochaOriginal.it.apply(this, wrapTestInZone(arguments)); };
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.xit = global.xspecify = Mocha.it.skip = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.it.only = global.test.only = Mocha.it.only = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.after = global.suiteTeardown = Mocha.after = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.afterEach = global.teardown = Mocha.afterEach = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.before = global.suiteSetup = Mocha.before = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments));
|
|
|
|
};
|
|
|
|
|
2020-01-31 08:54:44 -05:00
|
|
|
global.beforeEach = global.setup = Mocha.beforeEach = function() {
|
2019-05-31 11:56:07 -04:00
|
|
|
return mochaOriginal.beforeEach.apply(this, wrapTestInZone(arguments));
|
|
|
|
};
|
|
|
|
|
|
|
|
((originalRunTest, originalRun) => {
|
|
|
|
Mocha.Runner.prototype.runTest = function(fn: Function) {
|
|
|
|
Zone.current.scheduleMicroTask('mocha.forceTask', () => { originalRunTest.call(this, fn); });
|
|
|
|
};
|
|
|
|
|
|
|
|
Mocha.Runner.prototype.run = function(fn: Function) {
|
|
|
|
this.on('test', (e: any) => { testZone = rootZone.fork(new ProxyZoneSpec()); });
|
|
|
|
|
|
|
|
this.on('fail', (test: any, err: any) => {
|
|
|
|
const proxyZoneSpec = testZone && testZone.get('ProxyZoneSpec');
|
|
|
|
if (proxyZoneSpec && err) {
|
|
|
|
try {
|
|
|
|
// try catch here in case err.message is not writable
|
|
|
|
err.message += proxyZoneSpec.getAndClearPendingTasksInfo();
|
|
|
|
} catch (error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return originalRun.call(this, fn);
|
|
|
|
};
|
|
|
|
})(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run);
|
2020-01-31 08:54:44 -05:00
|
|
|
});
|