From c7572ac1f9bbda59ba301dd330e6f64faf91b9e1 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 2 Jun 2015 19:55:03 +0200 Subject: [PATCH] feat(fakeAsync): flush the microtasks before returning fixes #2269 --- modules/angular2/src/test_lib/fake_async.dart | 6 ++++-- modules/angular2/src/test_lib/fake_async.ts | 6 +++++- modules/angular2/test/test_lib/fake_async_spec.ts | 12 ++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/test_lib/fake_async.dart b/modules/angular2/src/test_lib/fake_async.dart index 9e8884c879..b7f4790bcb 100644 --- a/modules/angular2/src/test_lib/fake_async.dart +++ b/modules/angular2/src/test_lib/fake_async.dart @@ -27,7 +27,7 @@ Function fakeAsync(Function fn) { a7 = _u, a8 = _u, a9 = _u]) { // runZoned() to install a custom exception handler that re-throws return runZoned(() { - new quiver.FakeAsync().run((quiver.FakeAsync async) { + return new quiver.FakeAsync().run((quiver.FakeAsync async) { try { _fakeAsync = async; List args = [ @@ -42,7 +42,9 @@ Function fakeAsync(Function fn) { a8, a9 ].takeWhile((a) => a != _u).toList(); - return Function.apply(fn, args); + var res = Function.apply(fn, args); + _fakeAsync.flushMicrotasks(); + return res; } finally { _fakeAsync = null; } diff --git a/modules/angular2/src/test_lib/fake_async.ts b/modules/angular2/src/test_lib/fake_async.ts index e1b510decd..c77766854d 100644 --- a/modules/angular2/src/test_lib/fake_async.ts +++ b/modules/angular2/src/test_lib/fake_async.ts @@ -46,7 +46,11 @@ export function fakeAsync(fn: Function): Function { ListWrapper.clear(_pendingPeriodicTimers); ListWrapper.clear(_pendingTimers); - var res = fakeAsyncZone.run(() => { var res = fn(... args); }); + let res = fakeAsyncZone.run(() => { + let res = fn(... args); + flushMicrotasks(); + return res; + }); if (_pendingPeriodicTimers.length > 0) { throw new BaseException( diff --git a/modules/angular2/test/test_lib/fake_async_spec.ts b/modules/angular2/test/test_lib/fake_async_spec.ts index 0579677d63..26a226f945 100644 --- a/modules/angular2/test/test_lib/fake_async_spec.ts +++ b/modules/angular2/test/test_lib/fake_async_spec.ts @@ -46,6 +46,18 @@ export function main() { }); } + it('should flush microtasks before returning', () => { + var thenRan = false; + + fakeAsync(() => { PromiseWrapper.resolve(null).then(_ => { thenRan = true; }); })(); + + expect(thenRan).toEqual(true); + }); + + + it('should propagate the return value', + () => { expect(fakeAsync(() => 'foo')()).toEqual('foo'); }); + describe('Promise', () => { it('should run asynchronous code', fakeAsync(() => { var thenRan = false;