File tree Expand file tree Collapse file tree 4 files changed +71
-0
lines changed
tools/public_api_guard/core Expand file tree Collapse file tree 4 files changed +71
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @license
3
+ * Copyright Google Inc. All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+
9
+ import { jasmineAwait } from '../../testing' ;
10
+
11
+
12
+ describe ( 'await' , ( ) => {
13
+ let pass : boolean ;
14
+ beforeEach ( ( ) => pass = false ) ;
15
+ afterEach ( ( ) => expect ( pass ) . toBe ( true ) ) ;
16
+
17
+ it ( 'should convert passes' , jasmineAwait ( async ( ) => { pass = await Promise . resolve ( true ) ; } ) ) ;
18
+
19
+ it ( 'should convert failures' , ( done ) => {
20
+ const error = new Error ( ) ;
21
+ const fakeDone : DoneFn = function ( ) { fail ( 'passed, but should have failed' ) ; } as any ;
22
+ fakeDone . fail = function ( value : any ) {
23
+ expect ( value ) . toBe ( error ) ;
24
+ done ( ) ;
25
+ } ;
26
+ jasmineAwait ( async ( ) => {
27
+ pass = await Promise . resolve ( true ) ;
28
+ throw error ;
29
+ } ) ( fakeDone ) ;
30
+ } ) ;
31
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @license
3
+ * Copyright Google Inc. All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+
9
+ /**
10
+ * Converts an `async` function, with `await`, into a function which is compatible with Jasmine test
11
+ * framework.
12
+ *
13
+ * For asynchronous function blocks, Jasmine expects `it` (and friends) to take a function which
14
+ * takes a `done` callback. (Jasmine does not understand functions which return `Promise`.) The
15
+ * `jasmineAwait()` wrapper converts the test function returning `Promise` into a function which
16
+ * Jasmine understands.
17
+ *
18
+ *
19
+ * Example:
20
+ * ```
21
+ * it('...', jasmineAwait(async() => {
22
+ * doSomething();
23
+ * await asyncFn();
24
+ * doSomethingAfter();
25
+ * }));
26
+ * ```
27
+ *
28
+ */
29
+ export function jasmineAwait ( fn : ( ) => Promise < any > ) :
30
+ ( done : { ( ) : void ; fail : ( message ?: Error | string ) => void ; } ) => void {
31
+ return function ( done : { ( ) : void ; fail : ( message ?: Error | string ) => void ; } ) {
32
+ fn ( ) . then ( done , done . fail ) ;
33
+ } ;
34
+ }
Original file line number Diff line number Diff line change 13
13
*/
14
14
15
15
export * from './async' ;
16
+ export * from './jasmine_await' ;
16
17
export * from './component_fixture' ;
17
18
export * from './fake_async' ;
18
19
export * from './test_bed' ;
Original file line number Diff line number Diff line change @@ -53,6 +53,11 @@ export declare class InjectSetupWrapper {
53
53
inject ( tokens : any [ ] , fn : Function ) : ( ) => any ;
54
54
}
55
55
56
+ export declare function jasmineAwait ( fn : ( ) => Promise < any > ) : ( done : {
57
+ ( ) : void ;
58
+ fail : ( message ?: Error | string ) => void ;
59
+ } ) => void ;
60
+
56
61
/** @experimental */
57
62
export declare type MetadataOverride < T > = {
58
63
add ?: Partial < T > ;
You can’t perform that action at this time.
0 commit comments