Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feat(core): add support for using async/await with Jasmine (#24637)
Example: ``` it('...', await(async() => { doSomething(); await asyncFn(); doSomethingAfter(); })); ``` PR Close #24637
- Loading branch information
Showing
with
71 additions
and 0 deletions.
@@ -0,0 +1,31 @@ | ||
/** | ||
* @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 | ||
*/ | ||
|
||
import {jasmineAwait} from '../../testing'; | ||
|
||
|
||
describe('await', () => { | ||
let pass: boolean; | ||
beforeEach(() => pass = false); | ||
afterEach(() => expect(pass).toBe(true)); | ||
|
||
it('should convert passes', jasmineAwait(async() => { pass = await Promise.resolve(true); })); | ||
|
||
it('should convert failures', (done) => { | ||
const error = new Error(); | ||
const fakeDone: DoneFn = function() { fail('passed, but should have failed'); } as any; | ||
fakeDone.fail = function(value: any) { | ||
expect(value).toBe(error); | ||
done(); | ||
}; | ||
jasmineAwait(async() => { | ||
pass = await Promise.resolve(true); | ||
throw error; | ||
})(fakeDone); | ||
}); | ||
}); |
@@ -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); | ||
}; | ||
} |