Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(test): fix mocha compatible issue (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Mar 14, 2018
1 parent ab72df6 commit c554e9f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ script:
- node_modules/.bin/gulp test/node
- node simple-server.js 2>&1> server.log&
- node ./test/webdriver/test.sauce.js
- yarn add [email protected] [email protected]
- yarn add [email protected] [email protected] [email protected]
- yarn test:phantomjs-single
- node_modules/.bin/karma start karma-dist-sauce-jasmine3.conf.js --single-run
- node_modules/.bin/karma start karma-build-sauce-selenium3-mocha.conf.js --single-run
- node_modules/.bin/gulp test/node
2 changes: 1 addition & 1 deletion karma-build-sauce-selenium3-mocha.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

module.exports = function (config) {
require('./karma-dist-mocha.conf.js')(config);
require('./sauce-selenium3.conf')(config);
require('./sauce-selenium3.conf')(config, ['SL_IE9']);
};
3 changes: 0 additions & 3 deletions lib/mocha/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@

Mocha.Runner.prototype.run = function(fn: Function) {
this.on('test', (e: any) => {
if (Zone.current !== rootZone) {
throw new Error('Unexpected zone: ' + Zone.current.name);
}
testZone = rootZone.fork(new ProxyZoneSpec());
});

Expand Down
48 changes: 48 additions & 0 deletions test/zone-spec/async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,52 @@ describe('AsyncTestZoneSpec', function() {
}));
});
});

describe('should be able to handle async for both beforeEach and it', () => {
let log: string[];
const AsyncTestZoneSpec = (Zone as any)['AsyncTestZoneSpec'];

function asyncTest(testBody: () => void, finishCallback: Function, failCallback: Function) {
return function() {
const proxyZoneSpec = Zone.current.get('ProxyZoneSpec');
if (!proxyZoneSpec) {
throw new Error('ProxyZone not found!');
}
const lastDelegate = proxyZoneSpec.getDelegate();
// construct AsyncTestZoneSpec in parent zone
// to prevent infinite loop
Zone.current.parent.run(() => {
proxyZoneSpec.setDelegate(new AsyncTestZoneSpec(() => {
proxyZoneSpec.setDelegate(lastDelegate);
finishCallback();
}, () => {
proxyZoneSpec.setDelegate(lastDelegate);
failCallback();
}), 'async');
});
testBody.apply(this, arguments);
};
}

beforeEach(asyncTest(() => {
log = [];
setTimeout(() => {
log.push('beforeEach');
}, 50);
}, () => {
expect(log).toEqual(['beforeEach']);
}, () => {
fail('should not fail');
}));

it('should support asyncTest with an async beforeEach', asyncTest(() => {
setTimeout(() => {
log.push('timeout');
}, 50);
}, () => {
expect(log).toEqual(['beforeEach', 'timeout']);
}, () => {
fail('should not fail');
}));
});
});

0 comments on commit c554e9f

Please sign in to comment.