Skip to content

Commit 332937e

Browse files
JiaLiPassionkara
authored andcommitted
feat: make mocha a zone module. (#34719)
PR Close #34719
1 parent 81241af commit 332937e

3 files changed

Lines changed: 33 additions & 24 deletions

File tree

packages/zone.js/MODULE.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Modules
22

3-
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,
4-
the below samples show how to disable some modules. You just need to define a few global variables
3+
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,
4+
the below samples show how to disable some modules. You just need to define a few global variables
55
before loading zone.js.
66

77
```
@@ -18,7 +18,7 @@ before loading zone.js.
1818

1919
Below is the full list of currently supported modules.
2020

21-
- Common
21+
- Common
2222

2323
|Module Name|Behavior with zone.js patch|How to disable|
2424
|--|--|--|
@@ -55,6 +55,12 @@ Below is the full list of currently supported modules.
5555
|handleUnhandledPromiseRejection|NodeJS handle unhandledPromiseRejection from ZoneAwarePromise|__Zone_disable_handleUnhandledPromiseRejection = true|
5656
|crypto|NodeJS patch crypto function as macroTask|__Zone_disable_crypto = true|
5757

58+
- Test Framework
59+
|Module Name|Behavior with zone.js patch|How to disable|
60+
|--|--|--|
61+
|Jasmine|Jasmine APIs patch|__Zone_disable_jasmine = true|
62+
|Mocha|Mocha APIs patch|__Zone_disable_mocha = true|
63+
5864
- on_property
5965

6066
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.
8086

8187
By default, `zone.js/dist/zone-error` will not be loaded for performance concern.
8288
This package will provide following functionality.
83-
89+
8490
1. Error inherit: handle `extend Error` issue.
8591
```
8692
class MyError extends Error {}
@@ -104,7 +110,7 @@ This package will provide following functionality.
104110
105111
with this patch, those zone frames will be removed,
106112
and the zone information `<angular>/<root>` will be added
107-
113+
108114
```
109115
at a.b.c (vendor.bundle.js: 12345 <angular>)
110116
at d.e.f (main.bundle.js: 23456 <root>)
@@ -114,22 +120,22 @@ This package will provide following functionality.
114120
The flag is `__Zone_Error_BlacklistedStackFrames_policy`. And the available options is:
115121
116122
1. default: this is the default one, if you load `zone.js/dist/zone-error` without
117-
setting the flag, `default` will be used, and `BlackListStackFrames` will be available
118-
when `new Error()`, you can get a `error.stack` which is `zone stack free`. But this
123+
setting the flag, `default` will be used, and `BlackListStackFrames` will be available
124+
when `new Error()`, you can get a `error.stack` which is `zone stack free`. But this
119125
will slow down `new Error()` a little bit.
120126
121127
2. disable: this will disable `BlackListZoneStackFrame` feature, and if you load
122128
`zone.js/dist/zone-error`, you will only get a `wrapped Error` which can handle
123129
`Error inherit` issue.
124130
125131
3. lazy: this is a feature to let you be able to get `BlackListZoneStackFrame` feature,
126-
but not impact performance. But as a trade off, you can't get the `zone free stack
132+
but not impact performance. But as a trade off, you can't get the `zone free stack
127133
frames` by access `error.stack`. You can only get it by access `error.zoneAwareStack`.
128-
134+
129135
130136
- Angular(2+)
131137
132-
Angular uses zone.js to manage async operations and decide when to perform change detection. Thus, in Angular,
138+
Angular uses zone.js to manage async operations and decide when to perform change detection. Thus, in Angular,
133139
the following APIs should be patched, otherwise Angular may not work as expected.
134140
135141
1. ZoneAwarePromise

packages/zone.js/lib/mocha/mocha.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
'use strict';
1010

11-
((context: any) => {
12-
const Mocha = context.Mocha;
11+
Zone.__load_patch('mocha', (global: any, Zone: ZoneType) => {
12+
const Mocha = global.Mocha;
1313

1414
if (typeof Mocha === 'undefined') {
15-
throw new Error('Missing Mocha.js');
15+
// return if Mocha is not available, because now zone-testing
16+
// will load mocha patch with jasmine/jest patch
17+
return;
1618
}
1719

1820
if (typeof Zone === 'undefined') {
@@ -97,42 +99,42 @@
9799
return modifyArguments(args, syncTest, asyncTest);
98100
}
99101

100-
context.describe = context.suite = Mocha.describe = function() {
102+
global.describe = global.suite = Mocha.describe = function() {
101103
return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments));
102104
};
103105

104-
context.xdescribe = context.suite.skip = Mocha.describe.skip = function() {
106+
global.xdescribe = global.suite.skip = Mocha.describe.skip = function() {
105107
return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments));
106108
};
107109

108-
context.describe.only = context.suite.only = Mocha.describe.only = function() {
110+
global.describe.only = global.suite.only = Mocha.describe.only = function() {
109111
return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments));
110112
};
111113

112-
context.it = context.specify = context.test =
114+
global.it = global.specify = global.test =
113115
Mocha.it = function() { return mochaOriginal.it.apply(this, wrapTestInZone(arguments)); };
114116

115-
context.xit = context.xspecify = Mocha.it.skip = function() {
117+
global.xit = global.xspecify = Mocha.it.skip = function() {
116118
return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments));
117119
};
118120

119-
context.it.only = context.test.only = Mocha.it.only = function() {
121+
global.it.only = global.test.only = Mocha.it.only = function() {
120122
return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments));
121123
};
122124

123-
context.after = context.suiteTeardown = Mocha.after = function() {
125+
global.after = global.suiteTeardown = Mocha.after = function() {
124126
return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments));
125127
};
126128

127-
context.afterEach = context.teardown = Mocha.afterEach = function() {
129+
global.afterEach = global.teardown = Mocha.afterEach = function() {
128130
return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments));
129131
};
130132

131-
context.before = context.suiteSetup = Mocha.before = function() {
133+
global.before = global.suiteSetup = Mocha.before = function() {
132134
return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments));
133135
};
134136

135-
context.beforeEach = context.setup = Mocha.beforeEach = function() {
137+
global.beforeEach = global.setup = Mocha.beforeEach = function() {
136138
return mochaOriginal.beforeEach.apply(this, wrapTestInZone(arguments));
137139
};
138140

@@ -158,4 +160,4 @@
158160
return originalRun.call(this, fn);
159161
};
160162
})(Mocha.Runner.prototype.runTest, Mocha.Runner.prototype.run);
161-
})(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global);
163+
});

packages/zone.js/lib/testing/zone-testing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '../zone-spec/proxy';
1212
import '../zone-spec/sync-test';
1313
import '../jasmine/jasmine';
1414
import '../jest/jest';
15+
import '../mocha/mocha';
1516
import './async-testing';
1617
import './fake-async';
1718
import './promise-testing';

0 commit comments

Comments
 (0)