feat(fakeAsync): flush the microtasks before returning

fixes #2269
This commit is contained in:
Victor Berchet 2015-06-02 19:55:03 +02:00
parent ec3a78289f
commit c7572ac1f9
3 changed files with 21 additions and 3 deletions

View File

@ -27,7 +27,7 @@ Function fakeAsync(Function fn) {
a7 = _u, a8 = _u, a9 = _u]) { a7 = _u, a8 = _u, a9 = _u]) {
// runZoned() to install a custom exception handler that re-throws // runZoned() to install a custom exception handler that re-throws
return runZoned(() { return runZoned(() {
new quiver.FakeAsync().run((quiver.FakeAsync async) { return new quiver.FakeAsync().run((quiver.FakeAsync async) {
try { try {
_fakeAsync = async; _fakeAsync = async;
List args = [ List args = [
@ -42,7 +42,9 @@ Function fakeAsync(Function fn) {
a8, a8,
a9 a9
].takeWhile((a) => a != _u).toList(); ].takeWhile((a) => a != _u).toList();
return Function.apply(fn, args); var res = Function.apply(fn, args);
_fakeAsync.flushMicrotasks();
return res;
} finally { } finally {
_fakeAsync = null; _fakeAsync = null;
} }

View File

@ -46,7 +46,11 @@ export function fakeAsync(fn: Function): Function {
ListWrapper.clear(_pendingPeriodicTimers); ListWrapper.clear(_pendingPeriodicTimers);
ListWrapper.clear(_pendingTimers); 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) { if (_pendingPeriodicTimers.length > 0) {
throw new BaseException( throw new BaseException(

View File

@ -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', () => { describe('Promise', () => {
it('should run asynchronous code', fakeAsync(() => { it('should run asynchronous code', fakeAsync(() => {
var thenRan = false; var thenRan = false;