Skip to content

Commit 71100e6

Browse files
committed
feat(core): add support for using async/await with Jasmine (#24637)
Example: ``` it('...', await(async() => { doSomething(); await asyncFn(); doSomethingAfter(); })); ``` PR Close #24637
1 parent 676ec41 commit 71100e6

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

packages/core/testing/src/testing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
export * from './async';
16+
export * from './jasmine_await';
1617
export * from './component_fixture';
1718
export * from './fake_async';
1819
export * from './test_bed';

tools/public_api_guard/core/testing.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export declare class InjectSetupWrapper {
5353
inject(tokens: any[], fn: Function): () => any;
5454
}
5555

56+
export declare function jasmineAwait(fn: () => Promise<any>): (done: {
57+
(): void;
58+
fail: (message?: Error | string) => void;
59+
}) => void;
60+
5661
/** @experimental */
5762
export declare type MetadataOverride<T> = {
5863
add?: Partial<T>;

0 commit comments

Comments
 (0)