feat: make mocha a zone module. (#34719)

PR Close #34719
This commit is contained in:
JiaLiPassion 2020-01-31 22:54:44 +09:00 committed by Kara Erickson
parent 81241af7ac
commit 332937ef24
3 changed files with 33 additions and 24 deletions

View File

@ -1,7 +1,7 @@
# Modules # Modules
Starting from zone.js v0.8.9, you can choose which web API modules you want to patch as to reduce overhead introduced by the patching of these modules. For example, Starting from zone.js v0.8.9, you can choose which web API modules you want to patch as to reduce overhead introduced by the patching of these modules. For example,
the below samples show how to disable some modules. You just need to define a few global variables the below samples show how to disable some modules. You just need to define a few global variables
before loading zone.js. before loading zone.js.
``` ```
@ -18,7 +18,7 @@ before loading zone.js.
Below is the full list of currently supported modules. Below is the full list of currently supported modules.
- Common - Common
|Module Name|Behavior with zone.js patch|How to disable| |Module Name|Behavior with zone.js patch|How to disable|
|--|--|--| |--|--|--|
@ -55,6 +55,12 @@ Below is the full list of currently supported modules.
|handleUnhandledPromiseRejection|NodeJS handle unhandledPromiseRejection from ZoneAwarePromise|__Zone_disable_handleUnhandledPromiseRejection = true| |handleUnhandledPromiseRejection|NodeJS handle unhandledPromiseRejection from ZoneAwarePromise|__Zone_disable_handleUnhandledPromiseRejection = true|
|crypto|NodeJS patch crypto function as macroTask|__Zone_disable_crypto = true| |crypto|NodeJS patch crypto function as macroTask|__Zone_disable_crypto = true|
- Test Framework
|Module Name|Behavior with zone.js patch|How to disable|
|--|--|--|
|Jasmine|Jasmine APIs patch|__Zone_disable_jasmine = true|
|Mocha|Mocha APIs patch|__Zone_disable_mocha = true|
- on_property - on_property
You can also disable specific on_properties by setting `__Zone_ignore_on_properties` as follows: for example, You can also disable specific on_properties by setting `__Zone_ignore_on_properties` as follows: for example,
@ -80,7 +86,7 @@ you can do like this.
By default, `zone.js/dist/zone-error` will not be loaded for performance concern. By default, `zone.js/dist/zone-error` will not be loaded for performance concern.
This package will provide following functionality. This package will provide following functionality.
1. Error inherit: handle `extend Error` issue. 1. Error inherit: handle `extend Error` issue.
``` ```
class MyError extends Error {} class MyError extends Error {}
@ -104,7 +110,7 @@ This package will provide following functionality.
with this patch, those zone frames will be removed, with this patch, those zone frames will be removed,
and the zone information `<angular>/<root>` will be added and the zone information `<angular>/<root>` will be added
``` ```
at a.b.c (vendor.bundle.js: 12345 <angular>) at a.b.c (vendor.bundle.js: 12345 <angular>)
at d.e.f (main.bundle.js: 23456 <root>) at d.e.f (main.bundle.js: 23456 <root>)
@ -114,8 +120,8 @@ This package will provide following functionality.
The flag is `__Zone_Error_BlacklistedStackFrames_policy`. And the available options is: The flag is `__Zone_Error_BlacklistedStackFrames_policy`. And the available options is:
1. default: this is the default one, if you load `zone.js/dist/zone-error` without 1. default: this is the default one, if you load `zone.js/dist/zone-error` without
setting the flag, `default` will be used, and `BlackListStackFrames` will be available setting the flag, `default` will be used, and `BlackListStackFrames` will be available
when `new Error()`, you can get a `error.stack` which is `zone stack free`. But this when `new Error()`, you can get a `error.stack` which is `zone stack free`. But this
will slow down `new Error()` a little bit. will slow down `new Error()` a little bit.
2. disable: this will disable `BlackListZoneStackFrame` feature, and if you load 2. disable: this will disable `BlackListZoneStackFrame` feature, and if you load
@ -123,13 +129,13 @@ This package will provide following functionality.
`Error inherit` issue. `Error inherit` issue.
3. lazy: this is a feature to let you be able to get `BlackListZoneStackFrame` feature, 3. lazy: this is a feature to let you be able to get `BlackListZoneStackFrame` feature,
but not impact performance. But as a trade off, you can't get the `zone free stack but not impact performance. But as a trade off, you can't get the `zone free stack
frames` by access `error.stack`. You can only get it by access `error.zoneAwareStack`. frames` by access `error.stack`. You can only get it by access `error.zoneAwareStack`.
- Angular(2+) - Angular(2+)
Angular uses zone.js to manage async operations and decide when to perform change detection. Thus, in Angular, Angular uses zone.js to manage async operations and decide when to perform change detection. Thus, in Angular,
the following APIs should be patched, otherwise Angular may not work as expected. the following APIs should be patched, otherwise Angular may not work as expected.
1. ZoneAwarePromise 1. ZoneAwarePromise

View File

@ -8,11 +8,13 @@
'use strict'; 'use strict';
((context: any) => { Zone.__load_patch('mocha', (global: any, Zone: ZoneType) => {
const Mocha = context.Mocha; const Mocha = global.Mocha;
if (typeof Mocha === 'undefined') { if (typeof Mocha === 'undefined') {
throw new Error('Missing Mocha.js'); // return if Mocha is not available, because now zone-testing
// will load mocha patch with jasmine/jest patch
return;
} }
if (typeof Zone === 'undefined') { if (typeof Zone === 'undefined') {
@ -97,42 +99,42 @@
return modifyArguments(args, syncTest, asyncTest); return modifyArguments(args, syncTest, asyncTest);
} }
context.describe = context.suite = Mocha.describe = function() { global.describe = global.suite = Mocha.describe = function() {
return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments)); return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments));
}; };
context.xdescribe = context.suite.skip = Mocha.describe.skip = function() { global.xdescribe = global.suite.skip = Mocha.describe.skip = function() {
return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments)); return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments));
}; };
context.describe.only = context.suite.only = Mocha.describe.only = function() { global.describe.only = global.suite.only = Mocha.describe.only = function() {
return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments)); return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments));
}; };
context.it = context.specify = context.test = global.it = global.specify = global.test =
Mocha.it = function() { return mochaOriginal.it.apply(this, wrapTestInZone(arguments)); }; Mocha.it = function() { return mochaOriginal.it.apply(this, wrapTestInZone(arguments)); };
context.xit = context.xspecify = Mocha.it.skip = function() { global.xit = global.xspecify = Mocha.it.skip = function() {
return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments)); return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments));
}; };
context.it.only = context.test.only = Mocha.it.only = function() { global.it.only = global.test.only = Mocha.it.only = function() {
return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments)); return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments));
}; };
context.after = context.suiteTeardown = Mocha.after = function() { global.after = global.suiteTeardown = Mocha.after = function() {
return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments)); return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments));
}; };
context.afterEach = context.teardown = Mocha.afterEach = function() { global.afterEach = global.teardown = Mocha.afterEach = function() {
return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments)); return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments));
}; };
context.before = context.suiteSetup = Mocha.before = function() { global.before = global.suiteSetup = Mocha.before = function() {
return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments)); return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments));
}; };
context.beforeEach = context.setup = Mocha.beforeEach = function() { global.beforeEach = global.setup = Mocha.beforeEach = function() {
return mochaOriginal.beforeEach.apply(this, wrapTestInZone(arguments)); return mochaOriginal.beforeEach.apply(this, wrapTestInZone(arguments));
}; };
@ -158,4 +160,4 @@
return originalRun.call(this, fn); return originalRun.call(this, fn);
}; };
})(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run); })(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run);
})(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global); });

View File

@ -12,6 +12,7 @@ import '../zone-spec/proxy';
import '../zone-spec/sync-test'; import '../zone-spec/sync-test';
import '../jasmine/jasmine'; import '../jasmine/jasmine';
import '../jest/jest'; import '../jest/jest';
import '../mocha/mocha';
import './async-testing'; import './async-testing';
import './fake-async'; import './fake-async';
import './promise-testing'; import './promise-testing';