Skip to content

Commit

Permalink
fix(zone.js): patch global instead of Mocha object (#45047)
Browse files Browse the repository at this point in the history
Close #42834

In the new version fo Mocha, all global test functions are from `global`
object instead of `Mocha` object. Adn the current `zone.js` Mocha
patch's logic looks like this.

```
global.describe = Mocha.describe = function() {
  return originalMochaDescribe.apply(this, arguments);
}
```

and `originalMochaDescribe` is the unpathced Mocha implementation
looks like this

```
function describe() {
  return context.describe(...);
}
```

And the `context` will finally delegate to `global.describe()`,
so the current `zone.js` patch causes infinite loop.

This commit will not patch function of `Mocha` object any longer.

PR Close #45047
  • Loading branch information
JiaLiPassion authored and alxhub committed Feb 16, 2022
1 parent 6769f94 commit 8efbdb5
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions packages/zone.js/lib/mocha/mocha.ts
Expand Up @@ -40,12 +40,12 @@ Zone.__load_patch('mocha', (global: any, Zone: ZoneType) => {
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
after: global.after,
afterEach: global.afterEach,
before: global.before,
beforeEach: global.beforeEach,
describe: global.describe,
it: global.it
};

function modifyArguments(args: IArguments, syncTest: Function, asyncTest?: Function): any[] {
Expand Down Expand Up @@ -111,43 +111,43 @@ Zone.__load_patch('mocha', (global: any, Zone: ZoneType) => {
return modifyArguments(args, syncTest, asyncTest);
}

global.describe = global.suite = Mocha.describe = function() {
global.describe = global.suite = function() {
return mochaOriginal.describe.apply(this, wrapDescribeInZone(arguments));
};

global.xdescribe = global.suite.skip = Mocha.describe.skip = function() {
global.xdescribe = global.suite.skip = function() {
return mochaOriginal.describe.skip.apply(this, wrapDescribeInZone(arguments));
};

global.describe.only = global.suite.only = Mocha.describe.only = function() {
global.describe.only = global.suite.only = function() {
return mochaOriginal.describe.only.apply(this, wrapDescribeInZone(arguments));
};

global.it = global.specify = global.test = Mocha.it = function() {
global.it = global.specify = global.test = function() {
return mochaOriginal.it.apply(this, wrapTestInZone(arguments));
};

global.xit = global.xspecify = Mocha.it.skip = function() {
global.xit = global.xspecify = function() {
return mochaOriginal.it.skip.apply(this, wrapTestInZone(arguments));
};

global.it.only = global.test.only = Mocha.it.only = function() {
global.it.only = global.test.only = function() {
return mochaOriginal.it.only.apply(this, wrapTestInZone(arguments));
};

global.after = global.suiteTeardown = Mocha.after = function() {
global.after = global.suiteTeardown = function() {
return mochaOriginal.after.apply(this, wrapSuiteInZone(arguments));
};

global.afterEach = global.teardown = Mocha.afterEach = function() {
global.afterEach = global.teardown = function() {
return mochaOriginal.afterEach.apply(this, wrapTestInZone(arguments));
};

global.before = global.suiteSetup = Mocha.before = function() {
global.before = global.suiteSetup = function() {
return mochaOriginal.before.apply(this, wrapSuiteInZone(arguments));
};

global.beforeEach = global.setup = Mocha.beforeEach = function() {
global.beforeEach = global.setup = function() {
return mochaOriginal.beforeEach.apply(this, wrapTestInZone(arguments));
};

Expand Down

0 comments on commit 8efbdb5

Please sign in to comment.