fix(testing): async/fakeAsync/inject/withModule helpers should pass through context to callback functions (#13718)

Make sure that context (`this`) that is passed to functions generated by test helpers is passed through to the callback functions. Enables usage of Jasmine's variable sharing system to prevent accidental memory leaks during test runs.
This commit is contained in:
Dimitri Benin
2016-12-31 11:50:03 +01:00
committed by Miško Hevery
parent d69717cf79
commit 5f40e5ba21
5 changed files with 80 additions and 37 deletions

View File

@ -48,6 +48,7 @@ let _inFakeAsyncCall = false;
* @experimental
*/
export function fakeAsync(fn: Function): (...args: any[]) => any {
// Not using an arrow function to preserve context passed from call site
return function(...args: any[]) {
const proxyZoneSpec = ProxyZoneSpec.assertPresent();
if (_inFakeAsyncCall) {
@ -67,7 +68,7 @@ export function fakeAsync(fn: Function): (...args: any[]) => any {
const lastProxyZoneSpec = proxyZoneSpec.getDelegate();
proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
try {
res = fn(...args);
res = fn.apply(this, args);
flushMicrotasks();
} finally {
proxyZoneSpec.setDelegate(lastProxyZoneSpec);