Skip to content

Commit 70e8b40

Browse files
atscottthePunderWoman
authored andcommitted
fix(zone.js): Update the default behavior of fakeAsync to flush after the test (angular#57240)
From the internal issue on the matter: > When using the standard Jasmine version of it promises returned by the body function are automatically awaited. The Catalyst version of it is fake-async, so awaiting the promise does not make sense; however it would be nice if Catalyst automatically flushed the promise to replicate the experience of using standard it. This would allow users to do the following: ``` it('should fail later', async () => { await new Promise(r => setTimeout(r)); fail('failure'); }); ``` > In Catalyst today the above test will pass. If this proposal to automatically flush the resulting promise were implemented it would fail. Flushing after the tests complete has been the default behavior inside Google since 2020. Very few tests remain that use the old behavior of only flushing microtasks. The example above would actually fail with `fakeAsync` due to the pending timer, but the argument still remains the same. We might as well just flush if we're going to fail the test anyways by throwing if there's no flush at the end. BREAKING CHANGE: `fakeAsync` will now flush pending timers at the end of the given function by default. To opt-out of this, you can use `{flush: false}` in options parameter of `fakeAsync` PR Close angular#57240
1 parent a55ecb9 commit 70e8b40

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

packages/core/test/fake_async_spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,23 @@ describe('fake async', () => {
204204

205205
it('should throw an error on dangling timers', () => {
206206
expect(() => {
207-
fakeAsync(() => {
208-
setTimeout(() => {}, 10);
209-
})();
207+
fakeAsync(
208+
() => {
209+
setTimeout(() => {}, 10);
210+
},
211+
{flush: false},
212+
)();
210213
}).toThrowError('1 timer(s) still in the queue.');
211214
});
212215

213216
it('should throw an error on dangling periodic timers', () => {
214217
expect(() => {
215-
fakeAsync(() => {
216-
setInterval(() => {}, 10);
217-
})();
218+
fakeAsync(
219+
() => {
220+
setInterval(() => {}, 10);
221+
},
222+
{flush: false},
223+
)();
218224
}).toThrowError('1 periodic timer(s) still in the queue.');
219225
});
220226

packages/zone.js/lib/zone-spec/fake-async-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ export function resetFakeAsyncZone() {
833833
* @experimental
834834
*/
835835
export function fakeAsync(fn: Function, options: {flush?: boolean} = {}): (...args: any[]) => any {
836-
const {flush = false} = options;
836+
const {flush = true} = options;
837837
// Not using an arrow function to preserve context passed from call site
838838
const fakeAsyncFn: any = function (this: unknown, ...args: any[]) {
839839
const ProxyZoneSpec = getProxyZoneSpec();

packages/zone.js/test/zone-spec/fake-async-test.spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,17 +1333,23 @@ describe('fake async', () => {
13331333

13341334
it('should throw an error on dangling timers', () => {
13351335
expect(() => {
1336-
fakeAsync(() => {
1337-
setTimeout(() => {}, 10);
1338-
})();
1336+
fakeAsync(
1337+
() => {
1338+
setTimeout(() => {}, 10);
1339+
},
1340+
{flush: false},
1341+
)();
13391342
}).toThrowError('1 timer(s) still in the queue.');
13401343
});
13411344

13421345
it('should throw an error on dangling periodic timers', () => {
13431346
expect(() => {
1344-
fakeAsync(() => {
1345-
setInterval(() => {}, 10);
1346-
})();
1347+
fakeAsync(
1348+
() => {
1349+
setInterval(() => {}, 10);
1350+
},
1351+
{flush: false},
1352+
)();
13471353
}).toThrowError('1 periodic timer(s) still in the queue.');
13481354
});
13491355

0 commit comments

Comments
 (0)