10
10
import type { } from 'zone.js' ;
11
11
12
12
const _Zone : any = typeof Zone !== 'undefined' ? Zone : null ;
13
- const fakeAsyncTestModule = _Zone && _Zone [ _Zone . __symbol__ ( 'fakeAsyncTest' ) ] ;
13
+ function getFakeAsyncTestModule ( ) {
14
+ return _Zone && _Zone [ _Zone . __symbol__ ( 'fakeAsyncTest' ) ] ;
15
+ }
14
16
15
- const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for the fakeAsync() test helper but could not be found.
16
- Please make sure that your environment includes zone.js/testing` ;
17
+ function withFakeAsyncTestModule ( fn : ( fakeAsyncTestModule : any ) => any ) : any {
18
+ const fakeAsyncTestModule = getFakeAsyncTestModule ( ) ;
19
+ if ( ! fakeAsyncTestModule ) {
20
+ throw new Error ( `zone-testing.js is needed for the fakeAsync() test helper but could not be found.
21
+ Please make sure that your environment includes zone.js/testing` ) ;
22
+ }
23
+ return fn ( fakeAsyncTestModule ) ;
24
+ }
17
25
18
26
/**
19
27
* Clears out the shared fake async zone for a test.
@@ -22,15 +30,12 @@ const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for
22
30
* @publicApi
23
31
*/
24
32
export function resetFakeAsyncZone ( ) : void {
25
- if ( fakeAsyncTestModule ) {
26
- return fakeAsyncTestModule . resetFakeAsyncZone ( ) ;
27
- }
28
- throw new Error ( fakeAsyncTestModuleNotLoadedErrorMessage ) ;
33
+ withFakeAsyncTestModule ( ( v ) => v . resetFakeAsyncZone ( ) ) ;
29
34
}
30
35
31
36
export function resetFakeAsyncZoneIfExists ( ) : void {
32
- if ( fakeAsyncTestModule && ( Zone as any ) [ 'ProxyZoneSpec' ] ?. isLoaded ( ) ) {
33
- fakeAsyncTestModule . resetFakeAsyncZone ( ) ;
37
+ if ( getFakeAsyncTestModule ( ) && ( Zone as any ) [ 'ProxyZoneSpec' ] ?. isLoaded ( ) ) {
38
+ getFakeAsyncTestModule ( ) . resetFakeAsyncZone ( ) ;
34
39
}
35
40
}
36
41
@@ -59,10 +64,7 @@ export function resetFakeAsyncZoneIfExists(): void {
59
64
* @publicApi
60
65
*/
61
66
export function fakeAsync ( fn : Function , options ?: { flush ?: boolean } ) : ( ...args : any [ ] ) => any {
62
- if ( fakeAsyncTestModule ) {
63
- return fakeAsyncTestModule . fakeAsync ( fn , options ) ;
64
- }
65
- throw new Error ( fakeAsyncTestModuleNotLoadedErrorMessage ) ;
67
+ return withFakeAsyncTestModule ( ( v ) => v . fakeAsync ( fn , options ) ) ;
66
68
}
67
69
68
70
/**
@@ -135,10 +137,7 @@ export function tick(
135
137
processNewMacroTasksSynchronously : true ,
136
138
} ,
137
139
) : void {
138
- if ( fakeAsyncTestModule ) {
139
- return fakeAsyncTestModule . tick ( millis , tickOptions ) ;
140
- }
141
- throw new Error ( fakeAsyncTestModuleNotLoadedErrorMessage ) ;
140
+ return withFakeAsyncTestModule ( ( m ) => m . tick ( millis , tickOptions ) ) ;
142
141
}
143
142
144
143
/**
@@ -153,10 +152,7 @@ export function tick(
153
152
* @publicApi
154
153
*/
155
154
export function flush ( maxTurns ?: number ) : number {
156
- if ( fakeAsyncTestModule ) {
157
- return fakeAsyncTestModule . flush ( maxTurns ) ;
158
- }
159
- throw new Error ( fakeAsyncTestModuleNotLoadedErrorMessage ) ;
155
+ return withFakeAsyncTestModule ( ( m ) => m . flush ( maxTurns ) ) ;
160
156
}
161
157
162
158
/**
@@ -165,10 +161,7 @@ export function flush(maxTurns?: number): number {
165
161
* @publicApi
166
162
*/
167
163
export function discardPeriodicTasks ( ) : void {
168
- if ( fakeAsyncTestModule ) {
169
- return fakeAsyncTestModule . discardPeriodicTasks ( ) ;
170
- }
171
- throw new Error ( fakeAsyncTestModuleNotLoadedErrorMessage ) ;
164
+ return withFakeAsyncTestModule ( ( m ) => m . discardPeriodicTasks ( ) ) ;
172
165
}
173
166
174
167
/**
@@ -177,8 +170,5 @@ export function discardPeriodicTasks(): void {
177
170
* @publicApi
178
171
*/
179
172
export function flushMicrotasks ( ) : void {
180
- if ( fakeAsyncTestModule ) {
181
- return fakeAsyncTestModule . flushMicrotasks ( ) ;
182
- }
183
- throw new Error ( fakeAsyncTestModuleNotLoadedErrorMessage ) ;
173
+ return withFakeAsyncTestModule ( ( m ) => m . flushMicrotasks ( ) ) ;
184
174
}
0 commit comments