Skip to content

Commit f7918f5

Browse files
atscottthePunderWoman
authored andcommitted
feat(core): Add 'flush' parameter option to fakeAsync to flush after the test (#57239)
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. PR Close #57239
1 parent 4d0842d commit f7918f5

4 files changed

Lines changed: 10 additions & 7 deletions

File tree

goldens/public-api/core/testing/index.api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ export { DeferBlockState }
7070
export function discardPeriodicTasks(): void;
7171

7272
// @public
73-
export function fakeAsync(fn: Function): (...args: any[]) => any;
73+
export function fakeAsync(fn: Function, options?: {
74+
flush?: boolean;
75+
}): (...args: any[]) => any;
7476

7577
// @public
7678
export function flush(maxTurns?: number): number;

integration/typings_test_ts55/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@types/jasmine": "file:../../node_modules/@types/jasmine",
2222
"rxjs": "file:../../node_modules/rxjs",
2323
"typescript": "5.5.2",
24-
"zone.js": "0.14.0"
24+
"zone.js": "0.15.0"
2525
},
2626
"scripts": {
2727
"test": "tsc"

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"peerDependencies": {
2222
"rxjs": "^6.5.3 || ^7.4.0",
23-
"zone.js": "~0.14.0"
23+
"zone.js": "~0.15.0"
2424
},
2525
"repository": {
2626
"type": "git",

packages/core/testing/src/fake_async.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ export function resetFakeAsyncZoneIfExists(): void {
3535
* - Microtasks are manually executed by calling `flushMicrotasks()`.
3636
* - Timers are synchronous; `tick()` simulates the asynchronous passage of time.
3737
*
38-
* If there are any pending timers at the end of the function, an exception is thrown.
39-
*
4038
* Can be used to wrap `inject()` calls.
4139
*
4240
* @param fn The function that you want to wrap in the `fakeAsync` zone.
41+
* @param options
42+
* - flush: When true, will drain the macrotask queue after the test function completes.
43+
* When false, will throw an exception at the end of the function if there are pending timers.
4344
*
4445
* @usageNotes
4546
* ### Example
@@ -53,9 +54,9 @@ export function resetFakeAsyncZoneIfExists(): void {
5354
*
5455
* @publicApi
5556
*/
56-
export function fakeAsync(fn: Function): (...args: any[]) => any {
57+
export function fakeAsync(fn: Function, options?: {flush?: boolean}): (...args: any[]) => any {
5758
if (fakeAsyncTestModule) {
58-
return fakeAsyncTestModule.fakeAsync(fn);
59+
return fakeAsyncTestModule.fakeAsync(fn, options);
5960
}
6061
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
6162
}

0 commit comments

Comments
 (0)