diff --git a/packages/zone.js/MODULE.md b/packages/zone.js/MODULE.md index cade0d1ad5..ea7923c7cb 100644 --- a/packages/zone.js/MODULE.md +++ b/packages/zone.js/MODULE.md @@ -1,7 +1,7 @@ # 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, -the below samples show how to disable some modules. You just need to define a few global variables +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 before loading zone.js. ``` @@ -18,7 +18,7 @@ before loading zone.js. Below is the full list of currently supported modules. -- Common +- Common |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| |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 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. This package will provide following functionality. - + 1. Error inherit: handle `extend Error` issue. ``` class MyError extends Error {} @@ -104,7 +110,7 @@ This package will provide following functionality. with this patch, those zone frames will be removed, and the zone information `/` will be added - + ``` at a.b.c (vendor.bundle.js: 12345 ) at d.e.f (main.bundle.js: 23456 ) @@ -114,8 +120,8 @@ This package will provide following functionality. 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 - 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 + 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 will slow down `new Error()` a little bit. 2. disable: this will disable `BlackListZoneStackFrame` feature, and if you load @@ -123,13 +129,13 @@ This package will provide following functionality. `Error inherit` issue. 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`. - + - 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. 1. ZoneAwarePromise diff --git a/packages/zone.js/lib/mocha/mocha.ts b/packages/zone.js/lib/mocha/mocha.ts index df6764788e..c67fd7d2f4 100644 --- a/packages/zone.js/lib/mocha/mocha.ts +++ b/packages/zone.js/lib/mocha/mocha.ts @@ -8,11 +8,13 @@ 'use strict'; -((context: any) => { - const Mocha = context.Mocha; +Zone.__load_patch('mocha', (global: any, Zone: ZoneType) => { + const Mocha = global.Mocha; 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') { @@ -97,42 +99,42 @@ 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)); }; - 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)); }; - 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)); }; - context.it = context.specify = context.test = + global.it = global.specify = global.test = 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)); }; - 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)); }; - context.after = context.suiteTeardown = Mocha.after = function() { + global.after = global.suiteTeardown = Mocha.after = function() { 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)); }; - context.before = context.suiteSetup = Mocha.before = function() { + global.before = global.suiteSetup = Mocha.before = function() { 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)); }; @@ -158,4 +160,4 @@ return originalRun.call(this, fn); }; })(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run); -})(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global); +}); diff --git a/packages/zone.js/lib/testing/zone-testing.ts b/packages/zone.js/lib/testing/zone-testing.ts index e8154dba82..d142f0d933 100644 --- a/packages/zone.js/lib/testing/zone-testing.ts +++ b/packages/zone.js/lib/testing/zone-testing.ts @@ -12,6 +12,7 @@ import '../zone-spec/proxy'; import '../zone-spec/sync-test'; import '../jasmine/jasmine'; import '../jest/jest'; +import '../mocha/mocha'; import './async-testing'; import './fake-async'; import './promise-testing';