fix(fakeAsync): have fakeAsync use Proxy zone. (#10797)
Closes #10503 It is possible for code in `beforeEach` to capture and fork a zone (for example creating `NgZone` in `beforeEach`). Subsequently the code in `it` may chose to do `fakeAsync`. The issue is that because the code in `it` can use `NgZone` from the `beforeEach`. it effectively can escape the `fakeAsync` zone. A solution is to run all of the test in `ProxyZone` which allows a test to dynamically replace the rules at any time. This allows the `beforeEach` to fork a zone, and then `it` to retroactively became `fakeAsync` zone.
This commit is contained in:
@ -32,7 +32,13 @@ export function async(fn: Function): (done: any) => any {
|
||||
// function when asynchronous activity is finished.
|
||||
if (_global.jasmine) {
|
||||
return (done: any) => {
|
||||
runInTestZone(fn, done, (err: string | Error) => {
|
||||
if (!done) {
|
||||
// if we run beforeEach in @angular/core/testing/testing_internal then we get no done
|
||||
// fake it here and assume sync.
|
||||
done = function() {};
|
||||
done.fail = function(e: any) { throw e; };
|
||||
}
|
||||
runInTestZone(fn, done, (err: any) => {
|
||||
if (typeof err === 'string') {
|
||||
return done.fail(new Error(<string>err));
|
||||
} else {
|
||||
@ -50,13 +56,52 @@ export function async(fn: Function): (done: any) => any {
|
||||
}
|
||||
|
||||
function runInTestZone(fn: Function, finishCallback: Function, failCallback: Function) {
|
||||
var AsyncTestZoneSpec = (Zone as any /** TODO #9100 */)['AsyncTestZoneSpec'];
|
||||
const currentZone = Zone.current;
|
||||
var AsyncTestZoneSpec = (Zone as any)['AsyncTestZoneSpec'];
|
||||
if (AsyncTestZoneSpec === undefined) {
|
||||
throw new Error(
|
||||
'AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
|
||||
'Please make sure that your environment includes zone.js/dist/async-test.js');
|
||||
}
|
||||
var testZoneSpec = new AsyncTestZoneSpec(finishCallback, failCallback, 'test');
|
||||
var testZone = Zone.current.fork(testZoneSpec);
|
||||
return testZone.run(fn);
|
||||
const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec'] as {
|
||||
get(): {setDelegate(spec: ZoneSpec): void; getDelegate(): ZoneSpec;};
|
||||
assertPresent: () => void;
|
||||
};
|
||||
if (ProxyZoneSpec === undefined) {
|
||||
throw new Error(
|
||||
'ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
|
||||
'Please make sure that your environment includes zone.js/dist/proxy-zone.js');
|
||||
}
|
||||
const proxyZoneSpec = ProxyZoneSpec.get();
|
||||
ProxyZoneSpec.assertPresent();
|
||||
// We need to create the AsyncTestZoneSpec outside the ProxyZone.
|
||||
// If we do it in ProxyZone then we will get to infinite recursion.
|
||||
const proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
|
||||
const previousDelegate = proxyZoneSpec.getDelegate();
|
||||
proxyZone.parent.run(() => {
|
||||
var testZoneSpec: ZoneSpec = new AsyncTestZoneSpec(
|
||||
() => {
|
||||
// Need to restore the original zone.
|
||||
currentZone.run(() => {
|
||||
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
||||
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
||||
proxyZoneSpec.setDelegate(previousDelegate);
|
||||
}
|
||||
finishCallback();
|
||||
});
|
||||
},
|
||||
(error: any) => {
|
||||
// Need to restore the original zone.
|
||||
currentZone.run(() => {
|
||||
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
||||
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
||||
proxyZoneSpec.setDelegate(previousDelegate);
|
||||
}
|
||||
failCallback(error);
|
||||
});
|
||||
},
|
||||
'test');
|
||||
proxyZoneSpec.setDelegate(testZoneSpec);
|
||||
});
|
||||
return Zone.current.runGuarded(fn);
|
||||
}
|
||||
|
Reference in New Issue
Block a user