feat(core): add support for using async/await with Jasmine (#24637)
Example: ``` it('...', await(async() => { doSomething(); await asyncFn(); doSomethingAfter(); })); ``` PR Close #24637
This commit is contained in:

committed by
Miško Hevery

parent
676ec411b9
commit
71100e6d72
34
packages/core/testing/src/jasmine_await.ts
Normal file
34
packages/core/testing/src/jasmine_await.ts
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* Converts an `async` function, with `await`, into a function which is compatible with Jasmine test
|
||||
* framework.
|
||||
*
|
||||
* For asynchronous function blocks, Jasmine expects `it` (and friends) to take a function which
|
||||
* takes a `done` callback. (Jasmine does not understand functions which return `Promise`.) The
|
||||
* `jasmineAwait()` wrapper converts the test function returning `Promise` into a function which
|
||||
* Jasmine understands.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* ```
|
||||
* it('...', jasmineAwait(async() => {
|
||||
* doSomething();
|
||||
* await asyncFn();
|
||||
* doSomethingAfter();
|
||||
* }));
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
export function jasmineAwait(fn: () => Promise<any>):
|
||||
(done: {(): void; fail: (message?: Error | string) => void;}) => void {
|
||||
return function(done: {(): void; fail: (message?: Error | string) => void;}) {
|
||||
fn().then(done, done.fail);
|
||||
};
|
||||
}
|
@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
export * from './async';
|
||||
export * from './jasmine_await';
|
||||
export * from './component_fixture';
|
||||
export * from './fake_async';
|
||||
export * from './test_bed';
|
||||
|
Reference in New Issue
Block a user